By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A map is a reference type: the zero value is nil, reading a missing key returns the zero value, and writing to a nil map panics. Create one with make(map[string]int) or a literal before you write to it. The comma-ok form v, ok := m[k] is how you tell 'absent' from 'present and zero' — for a counter map that distinction is the whole game.
Iteration order is deliberately randomized. That is not a bug to work around with a library; it forces you to sort keys when output must be stable. Relay's reports sort keys before printing, which also makes the golden files in tests deterministic.
A Go string is an immutable sequence of bytes, usually UTF-8. len(s) counts bytes, utf8.RuneCountInString(s) counts code points, and for _, r := range s decodes runes. Indexing s[0] gives a byte, not a character — for non-ASCII text that is exactly the bug you want to catch before it reaches a customer-visible field. Use strings.Builder for repeated concatenation so you do not allocate a new string per append.
package main
import (
"fmt"
"slices"
"strings"
"unicode/utf8"
)
func main() {
counts := map[string]int{}
for _, line := range []string{"api 500", "api 500", "worker 200", "api 404"} {
service, _, _ := strings.Cut(line, " ")
counts[service]++
}
keys := make([]string, 0, len(counts))
for key := range counts {
keys = append(keys, key)
}
slices.Sort(keys)
for _, key := range keys {
fmt.Printf("%s=%d\n", key, counts[key])
}
if value, ok := counts["billing"]; ok {
fmt.Println("billing", value)
} else {
fmt.Println("billing missing")
}
message := "café ✅"
fmt.Println(len(message), utf8.RuneCountInString(message))
for _, r := range message {
fmt.Printf("%c", r)
}
fmt.Println()
fmt.Println(strings.ToUpper("relay"), strings.HasPrefix(message, "caf"))
var path strings.Builder
for i, part := range []string{"relay", "api", "events"} {
if i > 0 {
path.WriteByte('/')
}
path.WriteString(part)
}
fmt.Println(path.String())
}The strings package covers almost every text task in a backend: Cut, Split, Fields, TrimSpace, HasPrefix, ReplaceAll, Join, Builder. Two habits keep string code fast and correct. First, work on byte offsets when the data is known ASCII and on runes when it is user text. Second, when you compare or search many times in a loop, build the pattern once outside the loop.
| Type | Zero value | Mutable? | Iteration |
|---|---|---|---|
map[K]V | nil (readable, not writable) | Yes, through assignment | Random order |
string | "" | No | range yields runes |
[]byte | nil | Yes | Indexing yields bytes |
[]rune | nil | Yes | Indexing yields runes |
strings.Builder | Ready to use | Yes, append-only | Call String() when done |
Key takeaways
Maps are reference types; the nil map is readable but not writable.
Use the comma-ok form to distinguish a missing key from a stored zero value.
Map iteration order is randomized, so sort keys when output must be stable.
len(string) counts bytes; range and utf8.RuneCountInString work in runes.
Use strings.Cut for simple splitting and strings.Builder for repeated concatenation.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.