0Pricing
Go Academy · Lesson

Defining and Implementing Interfaces

Implicit satisfaction and the empty interface

Defining and Implementing Interfaces is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is an Interface?

An interface in Go is a set of method signatures. Any type that implements all those methods implicitly satisfies the interface — no implements keyword needed. This is called structural typing or duck typing.

Declaring an Interface

Define an interface with the type keyword:

package main
import "fmt"

type Shape interface {
    Area() float64
    Perimeter() float64
}

type Circle struct{ Radius float64 }
func (c Circle) Area() float64      { return 3.14159 * c.Radius * c.Radius }
func (c Circle) Perimeter() float64 { return 2 * 3.14159 * c.Radius }

func main() {
    var s Shape = Circle{5}
    fmt.Printf("Area: %.2f\n", s.Area())
}

Implicit Satisfaction

No declaration of intent is needed. If a type has the right methods, it satisfies the interface automatically:

package main
import "fmt"

type Greeter interface{ Greet() string }

type English struct{}
func (e English) Greet() string { return "Hello" }

type Spanish struct{}
func (s Spanish) Greet() string { return "Hola" }

func say(g Greeter) { fmt.Println(g.Greet()) }

func main() {
    say(English{})
    say(Spanish{})
}

The Empty Interface

interface{} (or any in Go 1.18+) is satisfied by every type. It's used when the type is unknown at compile time:

package main
import "fmt"

func printAny(v any) { fmt.Printf("%T: %v\n", v, v) }

func main() {
    printAny(42)
    printAny("hello")
    printAny([]int{1,2,3})
}

Interface Values

An interface value holds two things: a concrete type and a concrete value. A nil interface has both set to nil:

package main
import "fmt"

type Animal interface{ Sound() string }
type Dog struct{}
func (d Dog) Sound() string { return "woof" }

func main() {
    var a Animal
    fmt.Println(a == nil) // true
    a = Dog{}
    fmt.Println(a == nil) // false
    fmt.Println(a.Sound())
}

Multiple Interface Satisfaction

A type can satisfy multiple interfaces simultaneously:

package main
import "fmt"

type Reader interface{ Read() string }
type Writer interface{ Write(string) }
type ReadWriter interface{ Reader; Writer }

type File struct{ content string }
func (f *File) Read() string       { return f.content }
func (f *File) Write(s string)     { f.content = s }

func main() {
    var rw ReadWriter = &File{}
    rw.Write("hello")
    fmt.Println(rw.Read())
}

Interface Assignment

A wider interface can hold a narrower concrete value. Assigning between interface types requires the concrete type to satisfy both:

package main
import "fmt"

type Stringer interface{ String() string }

type Point struct{ X, Y int }
func (p Point) String() string { return fmt.Sprintf("(%d,%d)", p.X, p.Y) }

func main() {
    var s Stringer = Point{3, 4}
    fmt.Println(s.String())
}

Nil Interface Trap

A non-nil interface holding a nil concrete value is NOT equal to nil:

package main
import "fmt"

type MyErr struct{}
func (e *MyErr) Error() string { return "oops" }

func bad() error {
    var e *MyErr // nil pointer
    return e     // returns non-nil interface!
}

func main() {
    err := bad()
    fmt.Println(err == nil) // false — common gotcha
}

Interfaces Enable Testability

Defining dependencies as interfaces allows swapping real implementations for mocks in tests:

package main
import "fmt"

type EmailSender interface{ Send(to, body string) error }

type MockSender struct{ Sent []string }
func (m *MockSender) Send(to, body string) error {
    m.Sent = append(m.Sent, to)
    return nil
}

func notify(s EmailSender, addr string) { s.Send(addr, "hello") }

func main() {
    mock := &MockSender{}
    notify(mock, "a@b.com")
    fmt.Println(mock.Sent) // [a@b.com]
}

Accept Interfaces, Return Structs

A Go best practice: function parameters should be interfaces (for flexibility), return types should be concrete types (so callers know what they get):

package main

type Logger interface{ Log(string) }

type FileLogger struct{}
func (f *FileLogger) Log(s string) {}

func NewFileLogger() *FileLogger { // returns concrete
    return &FileLogger{}
}

func process(l Logger) {} // accepts interface

func main() { process(NewFileLogger()) }

Keep Interfaces Small

The Go standard library favors tiny interfaces: io.Reader has one method, io.Writer has one method. Small interfaces are more composable and easier to mock.

Quick Check

Does a type need to declare that it implements an interface?

Recap: Interfaces

Key takeaways:

  • Interfaces define behavior, not data
  • Satisfaction is implicit — no implements keyword
  • Empty interface (any) accepts every type
  • Watch the nil interface trap
  • Keep interfaces small and focused

Practice Prompt

Define a Storage interface with Save(key, value string) error and Load(key string) (string, error). Implement it with a MemStorage backed by a map and test it.

Frequently asked questions

Is the “Defining and Implementing Interfaces” lesson free?

Yes — the full text of “Defining and Implementing Interfaces” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Defining and Implementing Interfaces”?

Implicit satisfaction and the empty interface You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Defining and Implementing Interfaces” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining and Implementing Interfaces
  2. Core Standard Library Interfaces
  3. Type Assertions and Type Switches
  4. Interface Composition and Best Practices
← Back to Go Academy