Interfaces in Go: behaviour without classes
Learn Go · Beginner · 10 min · After Structs and methods in Go
How can one function work with many different types?
1Guess
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()) } }2Read
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 writeimplements. 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.3Code
fmt.Stringer: a method that fmt calls
1 / 2package 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) }Read the program. What will it print?
4Read
any, assertions and errors
anyis 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,okis false instead of a panic when the type is wrong.erroris an interface too: any type withError() stringis an error.5Code
A type switch over any
1 / 2package 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) }Read the program. What will it print?
6Fix
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()) }7Say it back
8Mission · 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.
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
Published Dec 3, 2026 · Updated Dec 3, 2026