Distinguish generic aliases and version rules
Distinguish generic aliases and version rules
A generic type definition creates a new type family. A generic alias names another type family without creating a distinct type. Full generic alias support begins in Go 1.24. Go 1.23 provided experimental support, not a portable baseline. The executable examples here use Go 1.23.2 compatible alternatives.
package main
import ( "fmt")
type List[T any] []Ttype IntList = List[int]
func main() { var a List[int] = List[int]{1, 2} var b IntList = a fmt.Println(b)}IntList aliases one instantiated type, so assignment needs no conversion. It is not a generic alias. On Go 1.24+ the declaration type Seq[T any] = List[T] can alias the whole family. That newer declaration is source-verified against https://go.dev/doc/go1.24, but not executed on the local compiler.
Expected output:
[1 2]Compare a distinct defined type
package main
import ( "fmt")
type List[T any] []Ttype Queue[T any] List[T]
func main() { a := List[int]{3, 4} b := Queue[int](a) fmt.Printf("%T %T\n", a, b)}Queue[int] and List[int] are distinct defined types; explicit conversion works here because their underlying types match. An alias instead preserves identity. Choose a module go directive and toolchain supporting the language features you actually use.
Expected output:
main.List[int] main.Queue[int]Try it locally
Define Bag[T any] []T and non-generic alias StringBag = Bag[string]. Assign Bag[string]{"tea"} to StringBag and print it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Generics 07: Distinguish generic aliases and version rules
Check the lesson objective before moving on.
When did full generic alias support arrive?
Does a type alias create a distinct type identity?