Interfaces

An interface value is a two-slot box: which type is inside and the value itself.

The idea

An interface is a job description: “anyone who can do Area()”. A rectangle can do it, a circle can do it, and neither has to announce that it “implements” the interface. If the methods are there, it fits.

Inside, an interface variable is a box with two slots: a type label (what is actually stored) and the value. When you call s.Area(), Go reads the type slot, finds that type’s Area, and runs it on the value. An interface is nil only when both slots are empty, which is the source of a famous gotcha.

Watch it work

Try it

Common mistakes