Generics

Generic zero values

A variable declared with a type parameter receives the zero value of its actual type argument. Returning nil is not generally legal for an unconstrained parameter. Use var zero T when a generic result needs its default value. Example output: '0 "" true'.

Formal Syntax & Structure

go syntaxgo
var zero T

Working Example

Example Snippetgo
package main
import "fmt"
func zero[T any]() T { var z T; return z }func main() {  fmt.Printf("%d %q %t\n", zero[int](), zero[string](), zero[*int]() == nil)}
Go Reference