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?

  1. 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())
    	}
    }
    
    What does this program print?
  2. 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 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.

  3. 3Code

    fmt.Stringer: a method that fmt calls

    1 / 2
    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)
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    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.

  5. 5Code

    A type switch over any

    1 / 2
    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)
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 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())
    }
    
    The compiler refuses to assign Dog{} to Animal. Why?
  7. 7Say it back

  8. 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

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

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer