Mastered Go generics syntax and type constraints, enabling reusable algorithms while maintaining compile-time type safety.
Key Learnings:
- Type Parameters: Written in square brackets (e.g.,
[T any]) before parameter lists. - Constraints: Define what types are allowed. Standard constraints are in the
golang.org/x/exp/constraintspackage (likeOrderedfor<and>comparisons). - The
anyconstraint: An alias forinterface{}. - The
comparableconstraint: A built-in interface that allows operations like==and!=. - Underlying types: Using the
~symbol (e.g.,~intmatches any custom type whose underlying type isint).
Real-World Code Example
package generics
// MapKeys returns a slice of all keys in a map.
// Keys must be comparable, and values can be any type.
func MapKeys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
This utility function works on maps of strings to ints, ints to floats, or any comparable key, eliminating duplicate helper functions!