Interfaces in Go

From the Go cheat sheet · Interfaces · verified Jul 2026

Interfaces

Defining and implementing interfaces

go
// Define interface
type Shape interface {
    Area() float64
    Perimeter() float64
}

// Implement interface
type Rectangle struct {
    Width, Height float64
}

func (r Rectangle) Area() float64 {
    return r.Width * r.Height
}
🎯 Implicit interface implementation
📦 interface{} for any type
🔄 Type assertions and type switches
⚡ Small interfaces are better (Interface Segregation)

More Go tasks

Back to the full Go cheat sheet