字符串
- string是数据类型,不是引用或指针类型
- string是只读的byte slice,len函数可以它所包含的byte数
- string的byte数组可以存放任何数据
package string_pra
import "testing"
func TestString(t *testing.T){
var s string
t.Log(s)
s = "hello"
t.Log(len(s))
//s[1] = '3' //string是不可变的类型
s = "\xE4\xB8\xA5"
t.Log(s)
t.Log(len(s)) //len求出里面的byte数
}
Unicode UTF8
- Unicode是一种字符集
- UTF8是unicode的存储实现(转换为字节序列的规则)
package string_func
import (
"strconv"
"strings"
"testing"
)
func TestStringFunc(t *testing.T){
s := "A,B,C"
parts := strings.Split(s,",")
for _,s := range parts {
t.Log(s)
}
s = strings.Join(parts,"-")
t.Log(s)
}
func TestStringToInt(t *testing.T){
s := strconv.Itoa(10)
t.Log(s)
if i,err := strconv.Atoi("10"); err == nil{
t.Log(10 + i)
}
}