Implicit Implementation

In many other languages, you must explicitly declare that a class implements an interface (e.g., class Circle implements Shape). In Go, implementation is implicit.

No Keywords Required

If a type has the proper methods defined, it automatically fulfills the interface. There is no implements keyword.

type reader interface {
    read() string
}

type book struct {
    content string
}

// book now implicitly implements reader
func (b book) read() string {
    return b.content
}

Benefits

  • Decoupling: The implementation of a struct doesn’t need to know about the interfaces that might use it.
  • Flexibility: You can define an interface in your package to describe behavior for a type defined in a completely different package.