3 ms·
I’m not aware of any significant breakage due to Go’s handling of strings. Where would I read more about this?
by skybrian 5y ago
I’m not aware of any significant breakage due to Go’s handling of strings. Where would I read more about this?
- noisem4ker 5y agoGo presents strings as slices of bytes (chars). As long as it's ASCII, it's fine. However, when dealing with multi-byte UTF-8 characters (codepoints), the proper unit is the rune. So, before attempting to measure a string's length, or read its n-th character, one must remember and access the string as a slice of runes. s := "naïve" // bad fmt.Println(len(s)) // 6 fmt.Println(string(s[2])) // Ã // good r := []rune(s) fmt.Println(len(r)) // 5 fmt.Println(string(r[2])) // ï https://go.dev/play/p/YbMo49wU7vu https://go.dev/play/p/YbMo49wU7vu
- skybrian 5y agoYes, but I think most Go developers are aware of Go's quirks, so I'm wondering what bugs happen despite that awareness.