Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Keywords and Statements

The interface type

Reference for Go interface types: method sets, implicit implementation, embedding, type sets with unions and ~T, the nil interface trap and comparable.

An interface type describes behavior. It lists methods, and any type that has all of those methods implements the interface automatically; there is no implements keyword. A variable of interface type holds a pair of values: a concrete type and a value of that type.

Interfaces can also define a type set using unions and ~T terms. Such interfaces can only be used as constraints on type parameters, not as ordinary variable types. The empty interface interface{} (spelled any) is satisfied by every type.

Go
type Shape interface {
    Area() float64
    Perimeter() float64
}

Try it

Go Implicit implementation and the nil trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Embedding and type sets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
InterfaceType = "interface" "{" { InterfaceElem ";" } "}" .
InterfaceElem = MethodElem | TypeElem .
MethodElem    = MethodName Signature .
TypeElem      = TypeTerm { "|" TypeTerm } .
TypeTerm      = [ "~" ] Type .
Part Description
Method A name and signature. Methods in the same interface must have unique names.
Embedded interface Adds all its methods (and type restrictions). Overlapping identical methods are allowed since Go 1.14.
Union A | B The type set is the union of the terms.
~T All types whose underlying type is T.
comparable A predeclared constraint satisfied by types that support ==.

Usage notes

  • Method sets decide. A value type T has only its value-receiver methods; *T has both. This is why &Rect{} implements Shape in the example but Rect{} would not.
  • Nil interface vs nil pointer. An interface is nil only if both its type and value are nil. Returning a typed nil pointer as an interface produces a non-nil interface. Return a literal nil instead.
  • Accept interfaces, return structs. Define small interfaces where they are used, such as the one-method io.Reader. Large interfaces are hard to satisfy and to mock.
  • Type assertions and switches. Use v, ok := i.(T) to recover the concrete type and a type switch to branch on it.
  • Constraint-only interfaces. An interface with a union or comparable cannot be a variable type: var n Number is an error.
  • Comparison. Interface values are comparable with == if the dynamic types are identical and comparable; comparing interfaces that hold uncomparable types (such as slices) panics at run time.
  • Cost. Calling a method through an interface is a dynamic dispatch and may stop inlining; storing a non-pointer value in an interface can allocate.
  • Verify at compile time. var _ Shape = (*Rect)(nil) fails to compile if the type stops implementing the interface.