Type semantics

Basic and constraint-only interfaces

Basic interfaces can be ordinary value types. Interfaces containing nonbasic type terms or comparable constraints restrict type parameters but cannot be used as ordinary variable types. Example output: '4 6'.

Formal Syntax & Structure

go syntaxgo
interface { Read() int }; interface { ~int }

Working Example

Example Snippetgo
package main
import "fmt"
type Count int
func (c Count) Read() int              { return int(c) }func twice[T interface{ ~int }](x T) T { return x + x }func main() {  var v interface{ Read() int } = Count(4)  fmt.Println(v.Read(), twice(3))}
Go Reference