字符串
package string_test
import (
"testing"
)
func TestString(t *testing.T) {
var s string
t.Log(s) //初始化为默认零值“”
s = "hello"
t.Log(len(s))
//s[1] = '3' //string是不可变的byte slice
s = "\xE4\xB8\xA5" //可以存储任何二进制数据
//s = "\xE4\xBA\xBB\xFF"
t.Log(s)
t.Log(len(s))
s = "中"
t.Log(len(s)) //是byte数
c := []rune(s)
t.Log(len(c))
// t.Log("rune size:", unsafe.Sizeof(c[0]))
t.Logf("中 unicode %x", c[0])
t.Logf("中 UTF8 %x", s)
}
func TestStringToRune(t *testing.T) {
s := "中华人民共和国"
for _, c := range s {
t.Logf("%[1]c %[1]x ", c)
//t.Logf("%c %x", c, c)
}
}
package string_func_test
import (
"strconv"
"strings"
"testing"
)
func TestStringFn(t *testing.T) {
s := "A,B,C"
parts := strings.Split(s, ",")
for index, part := range parts {
t.Log(index, part)
}
t.Log(strings.Join(parts, "-"))
}
func TestConv(t *testing.T) {
//s := 10
s := strconv.Itoa(10)
t.Log("str" + s)
// b, _ := strconv.Atoi("10")
// t.Log(b)
if a, err := strconv.Atoi("100"); err == nil {
t.Log(a + 10)
}
}