Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I’m not aware of any significant breakage due to Go’s handling of strings. Where would I read more about this?


Go 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


Yes, but I think most Go developers are aware of Go's quirks, so I'm wondering what bugs happen despite that awareness.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: