Tsallaka zuwa abun ciki
Justice Tech School

Interfaces

An interface lists methods. Any type with those methods satisfies it automatically, with no implements keyword.

Example
package main

import "fmt"

type Speaker interface{ Speak() string }
type Dog struct{}
type Cat struct{}

func (Dog) Speak() string { return "Woof" }
func (Cat) Speak() string { return "Meow" }

func main() {
	for _, s := range []Speaker{Dog{}, Cat{}} {
		fmt.Println(s.Speak())
	}
}

Result

Woof
Meow

Exercise

Declare an interface.

type S  { Speak() string }

Quiz Test

How does a type satisfy an interface?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.