Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Designing and Placing Interfaces | Section
Struct-Oriented Design in Go

bookDesigning and Placing Interfaces

Swipe to show menu

When designing interfaces in Go, you should pay careful attention to their size, naming, and placement. Small, focused interfaces are easier to implement and test. For example, an interface with a single method is often more useful than a large interface with many unrelated methods. Naming should clearly communicate the purpose of the interface, often by describing a capability, such as Reader, Writer, or Logger.

Placement of interfaces is another important design consideration. Should you define your interface where it is implemented or where it is consumed? The answer often depends on your project’s structure and goals, but Go idioms favor defining interfaces on the consumer side. This approach keeps interfaces minimal and tailored to the needs of the consuming package, which increases flexibility and decouples your code from specific implementations.

consumer/logger.go

consumer/logger.go

provider/simplelogger.go

provider/simplelogger.go

main.go

main.go

copy
1234567891011
package consumer // Logger is defined in the consumer package. type Logger interface { Log(message string) } // DoWork uses a Logger to log messages. func DoWork(l Logger) { l.Log("Doing some work...") }

Defining interfaces where they are used, rather than where they are implemented, has several advantages. It allows you to specify exactly what behavior you need, without forcing implementers to conform to a larger, less-focused interface. This results in code that is easier to test, mock, and refactor. When the consumer owns the interface, you can swap implementations freely, which increases the flexibility and maintainability of your codebase.

question mark

What is a key benefit of defining interfaces on the consumer side rather than the provider side in Go?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 8

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 8
some-alt