The standard library
The packages that already do what you were about to write.
Topic 46 of 98 · 5 min to read · 45 min to build
After this topic you can
- Write a small program using
strings,strconvandsortinstead of hand-rolled loops. - Explain why reaching for the standard library first saves bugs.
- Fix a hand-written helper by replacing it with the stdlib function that already does it.
The packages you will use most
| Package | What you use it for |
|---|---|
strings, strconv | text handling and number parsing |
time | durations, deadlines, formatting. Read the docs once, properly: the layout string is 2006-01-02 |
slices, maps, cmp | sorting, searching, and containment without loops |
errors, fmt | Is, As, Join, and %w wrapping |
encoding/json | request and response bodies for your web service |
net/http | server and client — see Web services |
database/sql | the database interface — see Databases |
context | cancellation, everywhere |
log/slog | structured logging, stdlib since 1.21 |
os, io, bufio | files, readers, writers |
sync, sync/atomic | mutexes, once, waitgroups, counters |
testing | tests, benchmarks, fuzzing |
crypto/rand | tokens and IDs. Never math/rand for anything security-related. |
Note: A workable rule: write the concrete version. Write it again for the second type. When you find yourself writing it a third time, then generalise. And check the stdlib first.
slices.Sort, slices.Contains, maps.Keys and cmp.Or cover a lot of what people hand-roll.Try it yourself
press Run — this executes on the Go Playground
package mainimport ( "cmp" "fmt" "maps" "slices" "strings")type User struct { Name string Age int}func main() { xs := []int{5, 2, 9, 1} slices.Sort(xs) fmt.Println(xs, slices.Contains(xs, 9), slices.Index(xs, 5), slices.Max(xs)) users := []User{{"Zoe", 30}, {"Adam", 25}} slices.SortFunc(users, func(a, b User) int { return cmp.Compare(a.Name, b.Name) }) fmt.Println(users) m := map[string]int{"b": 2, "a": 1, "c": 3} // maps.Keys returns an iterator, not a slice; slices.Sorted collects it and sorts it. keys := slices.Sorted(maps.Keys(m)) // maps have no order, so sort them fmt.Println(keys) fmt.Println(strings.Fields(" spread out words ")) before, after, _ := strings.Cut("Bearer abc123", " ") fmt.Println(before, "|", after) fmt.Println(strings.EqualFold("Go", "GO"))}The short version
slices,mapsandcmp(Go 1.21+) replace many hand-written loops.crypto/randfor anything security-related.math/randis predictable.go docreads the whole library offline, source included.- The stdlib source is clear, idiomatic Go. Read it when you are curious.
Remember: Check the standard library before you write a helper. It is usually already there.
Build this before moving on
Replace a hand-written sort and a hand-written contains check with slices.SortFunc and slices.Contains.Coding challenge · checked by tests
Write
Initials(name): return the first letter of each word, in capitals. Words are separated by any amount of spaces. Use strings.Fields and strings.ToUpper. Example: Initials(" rohan kumar patel ") returns "RKP".Tab indents. Press Esc, then Tab, to leave the editor.
Then try these in your own editor
- 1Run
go doc net/http.ServeMuxandgo doc -src errors.Is. - 2Find three functions in
stringsyou did not know existed. - 3Read the source of
errors.Isand work out how it walks the chain.
Official documentation