Stdlib Essentials
Advanced · Standard Library & I/O
A tour of packages you’ll reach for constantly.
- stdlib1 —
encoding/json: struct tags + (un)marshaling - stdlib2 —
io.Reader/io.Writer+io.Copy - stdlib3 —
slices(Go 1.21+) generic helpers - stdlib4 —
time: the reference-layout parse/format scheme
Resources
Section titled “Resources”- Go by Example: JSON
- Go blog: The io.Reader interface
- pkg.go.dev: slices · time
- Go by Example: Time Formatting / Parsing
Exercises
Section titled “Exercises”stdlib1 test
Section titled “stdlib1 test”stdlib1 — encoding/json
Show hint
Struct tags map JSON keys to fields:
Name string `json:"full_name"`Age int `json:"user_age"`stdlib2 test
Section titled “stdlib2 test”stdlib2 — io.Reader / io.Writer
Show hint
io.Copy streams a Reader into a Writer:
if _, err := io.Copy(&buf, r); err != nil { return "", err }stdlib3 test
Section titled “stdlib3 test”stdlib3 — slices
Show hint
Sort ascending then reverse:
slices.Sort(out)slices.Reverse(out)stdlib4 test
Section titled “stdlib4 test”stdlib4 — time
Show hint
The layout string IS the reference date Mon Jan 2 2006:
return time.Parse("2006-01-02", s)stdlib5 test
Section titled “stdlib5 test”stdlib5 — strconv
Show hint
strconv.Atoi parses a base-10 int:
n, err := strconv.Atoi(s); if err != nil { return 0, err }; return n*2, nilstdlib6 test
Section titled “stdlib6 test”stdlib6 — regexp
Show hint
Compile once, then match:
re := regexp.MustCompile(`^[^@\s]+@[^@\s]+\.[^@\s]+$`)return re.MatchString(s)