Generics

Parameterized interfaces

A generic interface can describe behavior for a chosen type argument. Instantiating a basic generic interface yields an ordinary usable interface type. A method returning one concrete type does not automatically satisfy an interface returning another. Example output: '7'.

Formal Syntax & Structure

go syntaxgo
type Source[T any] interface { Get() T }

Working Example

Example Snippetgo
package main
import "fmt"
type Source[T any] interface{ Get() T }type Fixed int
func (f Fixed) Get() int { return int(f) }func main() {  var s Source[int] = Fixed(7)  fmt.Println(s.Get())}
Go Reference