Generics

Related type parameters

A constraint can relate a parameter’s underlying slice type to another parameter representing its element type. Inference can determine both from a named slice argument. This example requires nonempty input. Example output: 'Ada'.

Formal Syntax & Structure

go syntaxgo
func First[S ~[]E, E any](s S) E

Working Example

Example Snippetgo
package main
import "fmt"
type Names []string
func first[S ~[]E, E any](s S) E { return s[0] }func main() {  fmt.Println(first(Names{"Ada", "Lin"}))}
Go Reference