Resi Dwi Thawasa

TIL

Go 1.21 added min, max, and clear

After upgrading to Go 1.21, I deleted a few tiny helper functions I had been carrying around for years.

min and max are builtins now. They work on any ordered type and take two or more args:

lo := min(a, b)
hi := max(a, b, c)

clear is the other one. For a map it removes all keys. For a slice it zeroes every element (length stays the same):

clear(m) // empty map
clear(s) // all elements set to zero value

Small additions, but nice. No more copy-pasting a minInt helper into every project.

#go