In Go, an interface is a set of method signatures, and any type that has those methods satisfies it automatically. Polymorphism comes from calling those methods through the interface: the same code works with many concrete types, and the right method runs at run time. You define the behaviour a function needs as an interface, then pass any type that provides it.
This article covers defining interfaces, implicit satisfaction, dynamic dispatch, the empty interface and generics, type assertions and switches, and the common pitfalls such as nil interfaces.
An interface lists methods. A type implements it just by declaring those methods, with no implements keyword.
describe works with every current and future Shape. That is polymorphism in Go: one function, many types, no inheritance.
An interface value holds two things: the concrete type and the value. When you call s.Area(), Go looks up the method for the stored type and calls it. This is dynamic dispatch. Because the compiler only checks that the method set matches, you can define an interface after the types exist, even for types from another package:
fmt.Println uses fmt.Stringer this way, and any type with a String() string method is printed through it.
Method sets matter for satisfaction. A value of type T has only the value-receiver methods. A *T has both value and pointer receiver methods.
If a method changes state, use a pointer receiver and store a pointer in the interface.
To get the concrete type back, use a type assertion with the two-value form, which does not panic:
Use these sparingly. If you switch on many concrete types, the interface is probably missing a method.
interface{}, spelled any since Go 1.18, has no methods, so every type satisfies it. It loses type safety, so use it only when needed, such as in fmt.Println or JSON decoding. For code that works over many types with the same shape, generics with constraints are often better:
Use an interface when behaviour differs by type, and generics when the algorithm is the same and only the data type changes.
An interface holding a nil pointer is not a nil interface. This common bug appears with error returns:
Return a literal nil for the interface instead. Also remember that a large interface is a weak abstraction: prefer one to three methods, and define the interface in the package that uses it.
No. A type satisfies an interface if it has all the methods. You can add var _ Shape = Circle{} to make the compiler check it.
A struct holds data fields. An interface describes behaviour as method signatures and holds any value that provides it.
any?Rarely. It is acceptable for truly dynamic data such as decoded JSON, but a specific interface or a generic type parameter gives compile-time checks.
An interface is nil only when both its type and value are nil. Assigning a typed nil pointer gives a non-nil interface.
Go interfaces define behaviour as method sets and are satisfied implicitly, which gives polymorphism through dynamic dispatch without inheritance. Keep them small, define them where they are used, mind pointer receivers, use type switches sparingly, choose generics when only the type varies, and beware of typed nil values stored in interfaces.