LESSON · 1 OF 22

What is an interface

What is an interface

An interface is a named set of method signatures. It says nothing about data and nothing about how the work gets done - it only lists the methods a type must have. The interface is a contract written in terms of behaviour, not fields.

Here is the shape. It lists method names, their parameters, and their return types, and nothing else:

go
type Stringer interface {
    String() string
}

That says: any type with a method String() that takes no arguments and returns a string is a Stringer. Once a type satisfies the interface, you can use a value of that type anywhere a Stringer is expected.

The idea to hold onto: an interface is a list of method signatures, and nothing more.

Spin up a fresh environment and practice live.
go · fresh machine · ready in under a minute