🐹 Go (Golang)
Advanced
What are generics in Go and how do they work?
Answer
Generics (type parameters) were introduced in Go 1.18 and allow writing functions and types that work with any type satisfying a given constraint. Syntax: func Map[T, U any](s []T, f func(T) U) []U. Constraints are interfaces that restrict which types are accepted — the built-in comparable allows ==, and the golang.org/x/exp/constraints package provides Integer, Ordered, etc. You can also write inline union constraints: [T int | float64]. Generics are instantiated at compile time, producing specialized code per type (monomorphization), with some inlining to control binary size. They eliminate many uses of interface{} and unsafe type assertions.