Let’s finish up our discussion of assignability by looking at how type parameters are special.
Assignability
…
Additionally, if
x
’s typeV
orT
are type parameters,x
is assignable to a variable of typeT
if one of the following conditions applies:
x
is the predeclared identifiernil
,T
is a type parameter, andx
is assignable to each type inT
’s type set.
func foo[T *int | *int32](t T) {
t = nil
}
V
is not a named type,T
is a type parameter, andx
is assignable to each type inT
’s type set.
func foo[T int | int32](t T) {
t = 234
}
V
is a type parameter andT
is 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