Let’s finish up our discussion of assignability by looking at how type parameters are special.
Assignability
…
Additionally, if
x’s typeVorTare type parameters,xis assignable to a variable of typeTif one of the following conditions applies:
xis the predeclared identifiernil,Tis a type parameter, andxis assignable to each type inT’s type set.
func foo[T *int | *int32](t T) {
t = nil
}
Vis not a named type,Tis a type parameter, andxis assignable to each type inT’s type set.
func foo[T int | int32](t T) {
t = 234
}
Vis a type parameter andTis not a named type, and values of each type inV’s type set are assignable toT.
func foo[V int | int32](x V) {
var t any = x
}
Quotes from The Go Programming Language Specification Version of December 15, 2022