Generics

Type argument inference

The compiler often infers type arguments from call arguments and constraints. If required arguments cannot be inferred, supply them explicitly. Go 1.27 generalizes inference in assignment and conversion contexts. Example output: 'int string'.

Formal Syntax & Structure

go syntaxgo
func Identity[T any](v T) T; Identity(5)

Working Example

Example Snippetgo
package main
import "fmt"
func identity[T any](v T) T { return v }func main() {  fmt.Printf("%T %T\n", identity(5), identity("go"))}
Go Reference