Code in square brackets [T any] defines the type that the func argument will use (thing T)
func AnyExample[T any](thing T) {
fmt.Println(thing)
}
MyPrint("hello world")
MyPrint(42)
MyPrint(true)Constrain the func argument to either int or string.
func ContstraintsExample[T int | string](intOrString T) {
fmt.Println(intOrString)
}func MultiTypeExample[A any, B int | string](a A, b B) {
fmt.Printf("a: %+v, b: %+v", a, b)
}Constrain with an interface that must implement a method. [T Stringer] defines an interface type that needs to satisfy Stringer
type Stringer interface {
String() string
}
type myStringer struct{}
func (p myStringer) String() string {
return "hello"
}
func StringerExample[T Stringer](thing T) {
fmt.Println(thing.String())
}If a type can constrain to interfaces that need to define the functions on the interface, so as operators. This is what Go calls comparables.
Comparable defines the == and != operators on the type.
func NotComparableExample[T any](a, b T) {
fmt.Println(a == b)
}
NotComparableExample(1, 1) // invalid operation: a == b (incomparable types in type set)func ComparableExample[T comparable](a, b T) {
fmt.Println(a == b)
}
ComparableExample(1, 1) // true
ComparableExample(1, 2) // false- Examples: https://go.dev/play/p/IomNXLIBXGU
- Tutorial: https://bitfieldconsulting.com/golang/generics