Generics

Constraint intersections

Each interface element further restricts the type set. A union permits alternatives, while a method requirement intersects those alternatives with the types implementing that method. This constraint-only interface cannot be used as a runtime variable type. Example output: '4'.

Formal Syntax & Structure

go syntaxgo
interface { ~int | ~int64; String() string }

Working Example

Example Snippetgo
package main
import "fmt"
type Count int
func (n Count) String() string { return fmt.Sprint(int(n)) }
type Printable interface {  ~int | ~int64  String() string}
func label[T Printable](n T) string { return n.String() }func main() {  fmt.Println(label(Count(4)))}
Go Reference