我正在阅读Go Essentials:
Go中的字符串是字节的不可变序列(8位字节值),这与Python,C#,Java或Swift等语言(其中字符串为Unicode)不同。
我在玩以下代码:
s := "???"
b :=[]byte{0xe6, 0x97, 0xa5, 0xe6, 0x9c, 0xac, 0xe8, 0xaa, 0x9e}
fmt.Println(string(b) == s) // true
for i, runeChar := range b {
fmt.Printf("byte position %d: %#U\n", i, runeChar)
}
//byte position 0: U+00E6 'æ'
//byte position 1: U+0097
//byte position 2: U+00A5 '¥'
//byte position 3: U+00E6 'æ'
//byte position 4: U+009C
//byte position 5: U+00AC '¬'
//byte position 6: U+00E8 'è'
//byte position 7: U+00AA 'ª'
//byte position 8: U+009E
for i, runeChar := range string(b) {
fmt.Printf("byte position %d: %#U\n", i, runeChar)
}
//byte position 0: U+65E5 '?'
//byte position 3: U+672C '?'
//byte position 6: U+8A9E '?'
问题:
peterSO.. 5
您引用的是一个不可靠的可靠资源:Go Essentials:Strings。除其他外,没有提及Unicode代码点或UTF-8编码。
例如,
package main
import "fmt"
func main() {
s := "???"
fmt.Printf("Glyph: %q\n", s)
fmt.Printf("UTF-8: [% x]\n", []byte(s))
fmt.Printf("Unicode codepoint: %U\n", []rune(s))
}
游乐场:https : //play.golang.org/p/iaYd80Ocitg
输出:
Glyph: "???" UTF-8: [e6 97 a5 e6 9c ac e8 aa 9e] Unicode codepoint: [U+65E5 U+672C U+8A9E]
参考文献:
Go博客:Go中的字符串,字节,符文和字符
Go编程语言规范
Unicode常见问题解答:UTF-8,UTF-16,UTF-32和BOM
Unicode联盟
1> peterSO..:
您引用的是一个不可靠的可靠资源:Go Essentials:Strings。除其他外,没有提及Unicode代码点或UTF-8编码。
例如,
package main
import "fmt"
func main() {
s := "???"
fmt.Printf("Glyph: %q\n", s)
fmt.Printf("UTF-8: [% x]\n", []byte(s))
fmt.Printf("Unicode codepoint: %U\n", []rune(s))
}
游乐场:https : //play.golang.org/p/iaYd80Ocitg
输出:
Glyph: "???" UTF-8: [e6 97 a5 e6 9c ac e8 aa 9e] Unicode codepoint: [U+65E5 U+672C U+8A9E]
参考文献:
Go博客:Go中的字符串,字节,符文和字符
Go编程语言规范
Unicode常见问题解答:UTF-8,UTF-16,UTF-32和BOM
Unicode联盟