Goadvancedgo1.18

Generics (type parameters)

Write one function that works for many types, checked at compile time.

Go 1.18 added type parameters and constraints — generic Map/Filter/Reduce, comparable lookups, and reusable containers, all statically type-safe with zero reflection.

Generic Max with an ordered constraint

Gointro
0ms
72.5pear10
Reference · output compiled offline, not runnable in-browser

One Max body serves int, float64, and string — the compiler picks T from each call, so "apple" vs "pear" is decided by lexicographic byte order (pear wins). The cmp.Ordered constraint is what permits the > comparison; an unconstrained any would not compile.