---
title: "Interfaces in Go: behaviour without classes"
slug: "interfaces"
description: "Describe behaviour with an interface, let types satisfy it by having the methods, and use any, type switches and fmt.Stringer. Fix a missing method."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-go/interfaces"
---

# Interfaces in Go: behaviour without classes

How can one function work with many different types?

## Predict

```go
package main

import "fmt"

type Shape interface {
	Area() float64
}

type Rect struct{ W, H float64 }

type Square struct{ Side float64 }

func (r Rect) Area() float64 { return r.W * r.H }

func (s Square) Area() float64 { return s.Side * s.Side }

func main() {
	shapes := []Shape{Rect{W: 2, H: 3}, Square{Side: 4}}
	for _, s := range shapes {
		fmt.Println(s.Area())
	}
}
```

What does this program print?

- It does not compile, because Rect and Square never say they implement Shape
- 6 on one line, then 16 on the next
- 6 and 4

Answer: 6 on one line, then 16 on the next — Each value has an Area method, so both fit Shape. The loop calls the right one for each value.

Output:

```
6
16
```

Rect and Square both have Area() float64, so both are Shapes. The loop calls each value's own method.

## An interface is a set of methods

An **interface** lists methods: `type Shape interface { Area() float64 }`. Any type that has those methods satisfies it. You never write `implements`. The compiler checks the methods at the place where you use the value as that interface. This lets one function accept many types, as long as they share the behaviour it needs. Keep interfaces small: one or two methods is common.

## fmt.Stringer: a method that fmt calls

```go
package main

import "fmt"

type Temp float64

func (t Temp) String() string {
	return fmt.Sprintf("%.1f°C", float64(t))
}

func show(s fmt.Stringer) {
	fmt.Println("value:", s.String())
}

func main() {
	t := Temp(21.5)
	fmt.Println(t)
	show(t)
}
```

Output:

```
21.5°C
value: 21.5°C
```

`fmt.Stringer` is an interface with one method, `String() string`. `Temp` never mentions it, yet `Println` and `show` accept a `Temp`.

Playground: https://go.dev/play/p/u23ZruTjpsT

## any, assertions and errors

`any` is an interface with no methods, so every value fits it. To get the value back you use a **type assertion**, `n, ok := x.(int)`, or a **type switch**, `switch v := x.(type)`. With the two-value form, `ok` is false instead of a panic when the type is wrong. `error` is an interface too: any type with `Error() string` is an error.

## A type switch over any

```go
package main

import (
	"errors"
	"fmt"
)

func describe(x any) {
	switch v := x.(type) {
	case int:
		fmt.Println("int:", v+1)
	case string:
		fmt.Println("string of length", len(v))
	case error:
		fmt.Println("error:", v.Error())
	default:
		fmt.Printf("something else: %T\n", v)
	}
}

func main() {
	describe(41)
	describe("gopher")
	describe(errors.New("disk full"))
	describe(2.5)
}
```

Output:

```
int: 42
string of length 6
error: disk full
something else: float64
```

Inside each case, `v` has that case's type. In `default` it keeps the type of `x`.

Playground: https://go.dev/play/p/qOxnu5xketf

## Fix it

```go
package main

import "fmt"

type Animal interface {
	Name() string
	Sound() string
}

type Dog struct{}

func (Dog) Name() string { return "Dog" }

func main() {
	var a Animal = Dog{}
	fmt.Println(a.Name(), a.Sound())
}
```

The compiler refuses to assign Dog{} to Animal. Why?

- Dog needs an implements Animal line
- Dog has no Sound method, and Animal needs both Name and Sound
- Interface values cannot hold structs

Answer: Dog has no Sound method, and Animal needs both Name and Sound — Every method of the interface must exist on the type. Add the missing one.

Compiler message:

```
cannot use Dog{} (value of struct type Dog) as Animal value in variable declaration: Dog does not implement Animal (missing method Sound)
```

Fixed:

```go
package main

import "fmt"

type Animal interface {
	Name() string
	Sound() string
}

type Dog struct{}

func (Dog) Name() string { return "Dog" }

func (Dog) Sound() string { return "Woof" }

func main() {
	var a Animal = Dog{}
	fmt.Println(a.Name(), a.Sound())
}
```

Output:

```
Dog Woof
```

## Say it back

In your own words: what does it mean that Go interfaces are satisfied implicitly?

Our sentence: A type satisfies an interface just by having all its methods. It never declares that it does, so the interface can be written later or elsewhere.

## Mission (10 min)

Write an Animal interface with a Sound() string method and two types that satisfy it. Put one of each in a slice, loop over it and print every sound.

```sh
go run .
```

## Common questions

### Do I have to declare that a type implements an interface in Go?

No. A type satisfies an interface when it has all the methods the interface lists, and nothing else is needed. There is no implements keyword. The check happens where you use the value as that interface, so the compiler reports a missing method at that line. This also means you can define an interface after the types it describes already exist.

### What is the empty interface or any in Go?

The any type is an interface with no methods, so every value satisfies it. It is useful when a function must accept values of unknown type, such as fmt.Println. The cost is that the compiler no longer knows the type. You recover it with a type assertion or a type switch. Prefer a small named interface when you know the behaviour you need.

### What is the difference between a type assertion and a type switch?

A type assertion, x.(T), checks one type. With the two-value form, v, ok := x.(T), ok tells you whether it worked and nothing panics. A type switch checks several types in one statement, one case each, and binds the converted value inside each case. Use an assertion for one expected type and a switch when there are several.

## Sources

- https://go.dev/ref/spec#Interface_types (read 2026-12-03)
- https://go.dev/doc/effective_go#interfaces (read 2026-12-03)
- https://pkg.go.dev/fmt#Stringer (read 2026-12-03)

Measured with go1.27.4 darwin/arm64 on 2026-12-03.
