golang-pitfalls-strings

Installation
SKILL.md

Golang Pitfalls: Strings & Bytes

Source material: mistakes #36-41 from 100 Go Mistakes and How to Avoid Them (teivah/100-go-mistakes).

Apply these rules when manipulating strings in Go.

36. Not understanding the concept of rune (#36)

  • A charset is a set of characters; an encoding translates characters to binary.
  • A Go string references an immutable slice of arbitrary bytes. Source-code literals are UTF-8, but strings from elsewhere may not be.
  • A rune is a Unicode code point, encoded in UTF-8 using 1 to 4 bytes.
  • len(s) returns the number of bytes, not runes. ("hêllo" has 5 runes but len == 6.)

37. Inaccurate string iteration (#37)

  • for i := range s iterates over the starting byte index of each rune, not each rune.
  • s[i] returns a single byte — printing it corrupts multi-byte runes (e.g., ê prints as Ã).
  • To print all runes, use the value element: for i, r := range s { ... }.
  • To access the ith rune, convert to []rune: []rune(s)[i]. This conversion costs O(n) — avoid it in hot loops; prefer range with the value when iterating everything.
Installs
2
First Seen
9 days ago
golang-pitfalls-strings — fabianoflorentino/golang-agent-skills