Generics

Generic receiver methods

Methods on generic receiver types declare corresponding receiver type parameter names with constraints inherited from the receiver type. Independent method type parameters are a separate Go 1.27 feature. Example output: 'go'.

Formal Syntax & Structure

go syntaxgo
func (b Box[T]) Get() T

Working Example

Example Snippetgo
package main
import "fmt"
type Box[T any] struct{ Value T }
func (b Box[T]) Get() T { return b.Value }func main() {  b := Box[string]{Value: "go"}  fmt.Println(b.Get())}
Go Reference