Generic types
Context
Section titled “Context”You can define generic structs, interfaces, and type aliases. The type parameter appears after the type name in square brackets. Generic types can have methods, but methods cannot have additional type parameters (the receiver’s parameters are fixed).
Example
Section titled “Example”Define a generic Stack[T] with push and pop methods.
Code example
Section titled “Code example”package main
import "fmt"
type Stack[T any] struct { items []T}
func (s *Stack[T]) Push(item T) { s.items = append(s.items, item)}
func (s *Stack[T]) Pop() (T, bool) { if len(s.items) == 0 { var zero T return zero, false } last := s.items[len(s.items)-1] s.items = s.items[:len(s.items)-1] return last, true}
func main() { intStack := Stack[int]{} intStack.Push(10) intStack.Push(20) fmt.Println(intStack.Pop()) // 20, true fmt.Println(intStack.Pop()) // 10, true fmt.Println(intStack.Pop()) // 0, false}Output
Section titled “Output”20 true10 true0 false