100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Go Interfaces Cheat Sheet

Go Interfaces Cheat Sheet

Explains Go's implicit interface satisfaction, type assertions and type switches, the empty interface, and idiomatic small-interface design.

2 PagesIntermediateApr 12, 2026

Defining & Implementing

Interfaces are satisfied implicitly by matching methods.

go
type Shape interface {    Area() float64    Perimeter() float64}type Rectangle struct {    Width, Height float64}func (r Rectangle) Area() float64      { return r.Width * r.Height }func (r Rectangle) Perimeter() float64 { return 2 * (r.Width + r.Height) }// Rectangle implicitly satisfies Shape -- no "implements" keyword neededvar s Shape = Rectangle{Width: 3, Height: 4}fmt.Println(s.Area())   // 12

Type Assertions & Type Switches

Recovering the concrete type behind an interface value.

go
func describe(s Shape) {    if r, ok := s.(Rectangle); ok {         // Type assertion with ok-check        fmt.Println("It's a rectangle:", r.Width, r.Height)    }    switch v := s.(type) {                  // Type switch    case Rectangle:        fmt.Println("rectangle area:", v.Area())    case Circle:        fmt.Println("circle area:", v.Area())    default:        fmt.Println("unknown shape")    }}

Empty Interface & Stdlib Interfaces

The any type and well-known standard library contracts.

go
func PrintAny(v any) {          // "any" is an alias for interface{} (Go 1.18+)    fmt.Println(v)}// Common standard library interfacestype Stringer interface {    String() string}type Writer interface {    Write(p []byte) (n int, err error)}func (r Rectangle) String() string {    return fmt.Sprintf("Rectangle(%vx%v)", r.Width, r.Height)  // Satisfies fmt.Stringer}

Concepts

How Go interfaces differ from other languages.

  • Implicit satisfaction- A type implements an interface automatically by implementing its methods, no declaration needed
  • Interface value- Holds a (type, value) pair internally; a nil interface differs from an interface holding a nil pointer
  • Empty interface (any)- interface{} / any can hold a value of any type; it loses compile-time type safety
  • Type assertion- v.(T) panics if the type is wrong; v, ok := v.(T) returns ok=false instead of panicking
  • Type switch- switch v := x.(type) branches on the dynamic type of an interface value
  • Small interfaces- Idiomatic Go favors small, focused interfaces, like io.Reader with just one method
  • Interface embedding- Interfaces can embed other interfaces to compose larger contracts
Pro Tip

Accept interfaces, return concrete types — function parameters should be the narrowest interface needed (like io.Reader), while return values should usually be concrete structs so callers get full functionality.

Was this cheat sheet helpful?

Explore Topics

#GoInterfaces#GoInterfacesCheatSheet#Programming#Intermediate#DefiningImplementing#Type#Assertions#Switches#Testing#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet