# Composition with Go

DevFeed: [Composition with Go](<https://devfeed.tech/articles/composition-with-go-22112.md>)

Original publisher: [Read original article](<https://www.ardanlabs.com/blog/2015/09/composition-with-go.html>)

Published: 2015-09-13T00:00:00Z

Content type: tutorial

Language: en

Sources: [William Kennedy](<https://devfeed.tech/sources/william-kennedy.md>)

Topics: [Go Language](<https://devfeed.tech/topics/go-language.md>), [interfaces](<https://devfeed.tech/topics/interfaces.md>), [Code](<https://devfeed.tech/topics/code.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [apis](<https://devfeed.tech/tags/apis.md>), [ardan-labs](<https://devfeed.tech/tags/ardan-labs.md>), [blog](<https://devfeed.tech/tags/blog.md>), [code](<https://devfeed.tech/tags/code.md>), [go](<https://devfeed.tech/tags/go.md>), [go-programming](<https://devfeed.tech/tags/go-programming.md>), [golang](<https://devfeed.tech/tags/golang.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [interfaces](<https://devfeed.tech/tags/interfaces.md>), [programming](<https://devfeed.tech/tags/programming.md>), [readability](<https://devfeed.tech/tags/readability.md>), [types](<https://devfeed.tech/tags/types.md>)

## AI overview

This tutorial explains composition in Go beyond type embedding, showing how single-purpose types and embedded interfaces can support flexible, readable APIs and larger programs. It uses a contractor and house-renovation example to demonstrate the design.

## Source excerpt

Composition goes beyond the mechanics of type embedding. It's a paradigm we can leverage to design better APIs and to build larger programs from smaller parts. It all starts from the declaration and implementation of types that have a single purpose. Programs that are architected with composition in mind have a better chance to grow and adapt to changing needs. They are also much easier to read and reason about. To demonstrate these concepts, we will be reviewing the following program: Sample Code This code sample explores the mechanics behind embedding and provides us with an opportunity to discuss how, with composition, we can design for both flexibility and readability. Every identifier that is exported from a package makes up the package's API. This includes all the constants, variables, types, methods and functions that are exported. Comments are a frequently-overlooked aspect of every package's API, so be very clear and concise when communicating information to the user of the package. The example is long so let's break it down in pieces and walk our way through it. The idea behind this program is we have a contractor that we hired to renovate our house. In particular, there are some boards in the house that have rotted and need to be yanked out, as well as new boards that need to be nailed in. The contractor will be given a supply of nails, boards to work with and tools to perform the work. Listing 1 13 // Board represents a surface we can work on. 14 type Board struct { 15 NailsNeeded int 16 NailsDriven int 17 } In listing 1, we have the declaration of the Board type. A Board has two fields, the number of nails the board needs and the current number of nails driven into the board. Now, let's look at the interfaces that are declared: Listing 2 21 // NailDriver represents behavior to drive nails into a board. 22 type NailDriver interface { 23 DriveNail(nailSupply *int, b *Board) 24 } 25 26 // NailPuller represents behavior to remove nails into a board. 27 type