Generics

Generic instantiation

Instantiation substitutes type arguments and checks constraints, producing a nongeneric type or function. Generic types generally require explicit type arguments when used; calls may infer them. Example output: 'ok 8'.

Formal Syntax & Structure

go syntaxgo
Box[int]; Identity[string]

Working Example

Example Snippetgo
package main
import "fmt"
type Box[T any] struct{ Value T }
func identity[T any](v T) T { return v }func main() {  f := identity[string]  b := Box[int]{Value: 8}  fmt.Println(f("ok"), b.Value)}
Go Reference