Definitions of generic types

July 25, 2023

Type definitions

If the type definition specifies type parameters, the type name denotes a generic type. Generic types must be instantiated when they are used.

type List[T any] struct {
	next  *List[T]
	value T
}

We haven’t covered instantiation yet, but we will. For now, let’s just summarize: Instantiation is process by which a generic type’s type parameters are substituted with the actual type arguments to be used.

So for example, given the above definition of List[T any], then instantiating it with a type argument of int would result in an effective type of:

type List struct {
  Next *List
  value int
}

Quotes from The Go Programming Language Specification Version of December 15, 2022

Share this