Skip to content

Stdlib Essentials

Advanced · Standard Library & I/O

A tour of packages you’ll reach for constantly.

  • stdlib1encoding/json: struct tags + (un)marshaling
  • stdlib2io.Reader/io.Writer + io.Copy
  • stdlib3slices (Go 1.21+) generic helpers
  • stdlib4time: the reference-layout parse/format scheme

stdlib1 — encoding/json

Show hint
Struct tags map JSON keys to fields:
Name string `json:"full_name"`
Age int `json:"user_age"`

Source

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 }

Source

stdlib3 — slices

Show hint
Sort ascending then reverse:
slices.Sort(out)
slices.Reverse(out)

Source

stdlib4 — time

Show hint
The layout string IS the reference date Mon Jan 2 2006:
return time.Parse("2006-01-02", s)

Source

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, nil

Source

stdlib6 — regexp

Show hint
Compile once, then match:
re := regexp.MustCompile(`^[^@\s]+@[^@\s]+\.[^@\s]+$`)
return re.MatchString(s)

Source