golang 中的字符串转换和 Unicode

标签 string go unicode char byte

我正在阅读 Go Essentials :

String in Go is an immutable sequence of bytes (8-bit byte values) This is different than languages like Python, C#, Java or Swift where strings are 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 '語'

问题:

  1. 当转换为字符串时,Golang 从哪里获取用于编码字节数组的 Unicode? rune 是如何形成的? Golang编译器在编译过程中是否从文本文件编码中获取Unicode?

  2. 将 String 实现为字节数组而不是 Java 中的 utf-16 字符数组有哪些优点和缺点?

最佳答案

您引用的来源不可靠: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))
}

Playground :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]

引用资料:

The Go Blog: Strings, bytes, runes and characters in Go

The Go Programming Language Specification

Unicode FAQ: UTF-8, UTF-16, UTF-32 & BOM

The Unicode Consortium

关于golang 中的字符串转换和 Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50878423/

相关文章:

unicode - 尝试在 Elixir 中将 unicode 字符写入文件时出现 no_translation 错误

go - 为什么 Go 中的 io.Writer 没有 NopCloser?

xml - 将带有越南字符的数据导入 R

java - 如何将包含数字和字母的字符串分成仅数字?

c++ - 使用 stringstream 打印四舍五入的 float

c - 添加文件到/usr/local/include

testing - 生成具有测试/快速、尊重不变量的结构树

ruby - 如何在 ruby​​ 1.8 中用 ascii 替换 unicode 引号?

xml - 如何在多个 xml 文件中搜索 linux 中两个标签之间出现的字符串?

string - 将单元格内容与 Excel 中的字符串进行比较