25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →

Interactive Go · Lesson

Interfaces

An interface describes what a type can do, not what it is — a contract of method signatures:

type Attacker interface {
    Attack() int
}

Any type that has those methods satisfies the interface implicitly — there’s no implements keyword. If it has an Attack() int method, it is an Attacker. This lets you write code against behaviour, so a Goblin, a Dragon, or a Slime can all be passed wherever an Attacker is expected.

Your Quest

Give Goblin a Speak() string method that returns "Grrr!", so it satisfies the Speaker interface.

Expected output:

Grrr!

Hint

func (g Goblin) Speak() string { return "Grrr!" } — a value receiver is fine here.

Additional Scrolls of Wisdom