我正在嘗試string使用utf8庫有效地計算來自 utf-8 的符文。這個例子是否是最佳的,因為它不復制底層數據?https://golang.org/pkg/unicode/utf8/#example_DecodeRuneInStringfunc main() { str := "Hello, 世界" // let's assume a runtime-provided string for len(str) > 0 { r, size := utf8.DecodeRuneInString(str) fmt.Printf("%c %v\n", r, size) str = str[size:] // performs copy? }}我在(不安全的)反射庫中找到了StringHeader 。string這是Go 中a 的確切結構嗎?如果是這樣,可以想象對字符串進行切片只是更新Data或分配一個新的StringHeader字符串。type StringHeader struct {
Data uintptr
Len int
}獎勵:我在哪里可以找到執行切片的代碼string以便我自己查找?這些中的任何一個?https://golang.org/src/runtime/slice.gohttps://golang.org/src/runtime/string.goThis related SO answer表明運行時字符串從string轉換為時會產生一個副本[]byte。
- 0 回答
- 0 關注
- 112 瀏覽
添加回答
舉報
0/150
提交
取消