Skip to content

Modern

Intermediate · Generics & Modern Go

Recent language additions every current Go programmer should know.

  • modern1 — built-in min / max / clear (Go 1.21)
  • modern2 — range over an integer, for i := range n (Go 1.22)
  • modern3 — range-over-function iterators with iter.Seq (Go 1.23)

Go 1.21 added built-in min and max (any ordered type, two or more args) and

Show hint
Use the Go 1.21 builtins (no import):
lo = min(a, b, c)
hi = max(a, b, c)
clear(m)

Source

Go 1.22: a for-range loop can iterate over an integer directly —

Show hint
Go 1.22 lets you range over an int:
for i := range n { total += i }

Source

Go 1.23: for-range can iterate over an iterator function. An iter.Seq[V] is

Show hint
Call yield for each value; stop early if it returns false:
for i := 1; i <= n; i++ { if !yield(i) { return } }

Source