Design patterns in Golang — The Builder

I always wondered, how should we apply our very dear “Design Patterns” in Golang?

Object-Oriented Programming in Go is weird. If you are a Java or a C# developer, you would find a lot of similarities in applying SOLID principles in your code. But Go is special and special for a reason.

We would be taking a closer look at how we can implement some famous design patterns in Go.

Builder pattern

The motivation behind the Builder pattern is to create a complex object piece-by-piece. Our goal in this blog is to provide APIs to create complex objects step-by-step.


Some objects are easy to create with a simple constructor while others need a lot of ceremonies.

When do we need this?

When you don’t need your end-user to provide millions of details in parameters of a constructor and you want to make his/her life easy by providing them with a simple way to create a complex object.


  1. Builder facets
  2. Builder parameters
  3. Functional builders

Builder facets


They are more than one builder working in tandem to create an object.


Sometimes, our object is complex and the creational process requires more than one builder class. Segregating the builder into multiple builders also gives us control over the properties of a struct that a user can use.

Show me the code!


Suppose we want to create a fluent API that builds a person object like this:

We start with creating a Person struct and 3 builders PersonBuilder PersonAddressBuilder and PersonJobBuilder

Notice that thePersonAddressBuilder and PersonJobBuilder refer to PersonBuilder

Next, we would declare the constructors for these builders.

Notice how Lives() and Works() refer to PersonBuilder while initalization

Now, why this is important?

This would enable us to set properties of Person which is part of PersonBuilder

Ok, how would we set properties now?

Glad you asked!
We would create some behaviours/functions/methods for PersonAddressBuilder and PersonJobBuilder that sets the properties of thePerson

and finally, a “Build” method that returns a Person object.

So, putting it all together!


Once an object of PersonBuilder is created we switch to Lives() that returns a PersonAddressBuilder which have member functions like At() and WithPostalCode() , since we are returning pointers in all the function definitions we can chain our function calls together (line 91–97)

This way we can jump between different builders and use their corresponding member functions to create a complex object.

The result (trivial) looks like this:


Builder Parameters:

Functional Builders:




Learning the Feynman technique.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Changing the Color Scheme in Droopler with the help of SCSS

Integrating Map Services in React

8 best places to learn Javascript (2018 Updated Free & Paid)

How fast is your code?

Overview of ORM Database Tools

Django rest framework and Social Logins with python-social-auth

Frontend Weekly Digest #179 (5 — 11 October 2020)

How to manage multiple requests with “Promise.all”

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Surya Reddy

Surya Reddy

Learning the Feynman technique.

More from Medium

Generics in Golang.

Golang in sixty seconds — string formatting

How I set up my Go development environment

TicTacGo — An Intro to Golang