advanced20 min

Interfaces

How Go's implicit, structural interfaces differ from interfaces in most other languages.

What you'll learn

  • Define an interface as a set of method signatures
  • Explain that Go interfaces are satisfied implicitly, with no `implements` keyword
  • Recognize the empty interface `interface{}` (or `any`) as accepting any type

Explanation

A Go interface defines a set of method signatures a type must have: type Shaper interface { Area() float64 }. Any type with an Area() float64 method automatically satisfies Shaper -- there's no implements keyword, and the type doesn't need to reference the interface anywhere. This is called structural typing (or "duck typing" at compile time): if it has the right methods, it satisfies the interface.

This is a real, deliberate design difference from Java or C#, where a class must explicitly declare implements InterfaceName. In Go, you can define a new interface after a type already exists, and old types automatically satisfy it if their methods happen to match -- useful for writing flexible code against libraries you don't control.

The empty interface, written interface{} (or its modern alias any), has zero required methods, so every type satisfies it. It's Go's escape hatch for "accept a value of any type," though idiomatic Go code use it sparingly, preferring specific interfaces where practical since any gives up compile-time type safety.

Guided lab

Predict: Implicit interface satisfaction

GoNot executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints.

package main

import "fmt"

type Shaper interface {
	Area() float64
}

type Rectangle struct {
	Width, Height float64
}

func (r Rectangle) Area() float64 {
	return r.Width * r.Height
}

func describe(s Shaper) {
	fmt.Printf("Area: %.1f\n", s.Area())
}

func main() {
	rect := Rectangle{Width: 3, Height: 4}
	describe(rect)
}

Stuck? Get a hint.

Common mistakes

  • Looking for an `implements` keyword in Go -- it doesn't exist; interface satisfaction is entirely implicit and structural.
  • Overusing `any`/`interface{}` where a specific interface would give better compile-time safety.
  • Assuming a type must be declared with the interface in mind -- Go interfaces can be satisfied by types written with no knowledge of that interface.

Knowledge check

Knowledge check

1. How does a Go type declare that it implements an interface?
2. What does the empty interface `interface{}` (or `any`) accept?
3. Can a type satisfy a Go interface defined after the type itself already exists?

Takeaway

Go interfaces are satisfied implicitly by having the right methods -- there is no `implements` keyword, and types don't need to know about an interface to satisfy it.

Summary

Interfaces are sets of method signatures satisfied structurally/implicitly; the empty interface (`any`) accepts every type but should be used sparingly.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.

Next: Error Handling