在Go语言中,字符字面值以可变长度的UTF-8编码字节数序列的形式存储在字符串中。ASCII代码点(0x00.. 0x7F)占用一个字节。其他代码点占用两到四个字节。要单独打印代码点(字符),
package main
import "fmt"
func main() {
strslice := make([]string, 5, 5)
strslice[0] = "hello"
strslice[1] = "go"
strslice[2] = "lang"
strslice[3] = "whatsup"
strslice[4] = "Hello, 世界"
for _, s := range strslice {
for _, c := range s {
fmt.Printf("%c ", c)
}
fmt.Printf("\n")
}
}
输出:
h e l l o
g o
l a n g
w h a t s u p
H e l l o , 世 界
以下是对UTF-8编码字节和字符差异的说明示例:
package main
import "fmt"
func main() {
str := "Hello, 世界"
fmt.Println("Bytes:")
for i := 0; i < len(str); i++ {
fmt.Printf("'%c' ", str[i])
}
fmt.Printf("\n")
fmt.Println("Characters:")
for _, c := range str {
fmt.Printf("'%c' ", c)
}
fmt.Printf("\n")
}
输出:
Bytes:
'H' 'e' 'l' 'l' 'o' ',' ' ' 'ä' '¸' '' 'ç' '' ''
Characters:
'H' 'e' 'l' 'l' 'o' ',' ' ' '世' '界'
参考文献:
Unicode UTF-8 FAQ
For语句,Go编程语言规范