[]Rune
[]rune
是golang
的基本数据类型string
是只读的byte
数据,string
字符使用utf-8
编码,每个字符占位1~3 bytes
rune
占位4 bytes
- 对于英文字符,
string
和rune
类型没有区别 - 对于中文字符,
rune
类型占位4 bytes
字符,用于操作中文字符不会出现乱码
Examples:
// string & rune compare,
package main
import "fmt"
// string & rune compare,
func stringAndRuneCompare() {
// string,
s := "hello你好"
fmt.Printf("%s, type: %T, len: %d\n", s, s, len(s))
fmt.Printf("s[%d]: %v, type: %T\n", 0, s[0], s[0])
li := len(s) - 1 // last index,
fmt.Printf("s[%d]: %v, type: %T\n\n", li, s[li], s[li])
// []rune
rs := []rune(s)
fmt.Printf("%v, type: %T, len: %d\n", rs, rs, len(rs))
}
func main() {
stringAndRuneCompare()
}
OutPut:
hello你好, type: string, len: 11
s[0]: 104, type: uint8
s[10]: 189, type: uint8
[104 101 108 108 111 20320 22909], type: []int32, len: 7
Analysis:
hello你好
占位11 bytes(5 * 1 + 2 * 3 = 11)
string
转rune
时,一共 7 个 utf-8 字符,因此转换成 size = 7 的 []rune