Generics

Generic type sets and unions

Union terms combine permitted types; separate interface elements intersect their sets. Non-interface terms in a union must have disjoint sets. Operations on type parameters must obey the constraint and language rules. Example output: '6 8'.

Formal Syntax & Structure

go syntaxgo
interface { ~int | ~int64 }

Working Example

Example Snippetgo
package main
import "fmt"
type Count int
func double[T ~int | ~int64](x T) T { return x + x }func main() {  fmt.Println(double(Count(3)), double(int64(4)))}
Go Reference