Strings
Treat strings as immutable byte sequences that may contain UTF-8; choose byte- or rune-oriented operations deliberately.
Canonical guidance
- strings are immutable
- bytes and runes solve different problems
- slicing a string operates on bytes, not characters
Use when
- text processing
- protocol fields
- log and error messages
Avoid
- indexing strings as if every character were one byte
- converting between string and
[]byterepeatedly without reason - assuming all input is valid UTF-8
Preferred pattern
for _, r := range s {
fmt.Println(r)
}
Anti-pattern
- slicing strings by byte offsets when the logic really cares about user-visible characters
Explanation: This anti-pattern is tempting because byte indexing is simple, but it breaks once multibyte UTF-8 input appears.
Why
- string handling bugs are often really byte-vs-rune bugs
Related pages
Sources
- The Go Programming Language Specification - Go Team
- Effective Go - Go Team