Go Builder Pattern — The Functional Way

So, in my previous blogs, we talked about Builder Facets and Builder Parameters.

Read them here:

  1. Builder Facets
  2. Builder Parameters

One way to extend the builder that you already have is by using a Functional Programming approach!

Let’s take the same example that we used in my previous blog.

We want to create an Employee object step by step using a functional approach, that would look something like this!

To achieve a fluent API like this in “go” is really simple.


  1. We add a property to the Builder struct that takes an array of actions. (line-16)
  2. Every builder’s member function adds an action to the above property. (line 20–41)
  3. The Build() function iterates through all the actions and executes it on an Employee object. (line 44–50)

In Summary:

Builder patterns are useful but sometimes can be a pain in wrong places. The upside is being able to create complex objects more easily with control over what properties to expose and what to hide from the end-user, but the downside is whenever a new property is added we need to create a new function to set the value.

Builder Facets can be used to functionally break the builder in smaller sub-builders that can work in tandem to create a complex object.

Builder Parameters can be used to have control over the properties that we want to expose to our developers.

Functional way of creating builders enable us to create fluent APIs that are easy to read.




Learning the Feynman technique. https://devcharmander.me/

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

Recommended from Medium

Part 2 — Manage Payment Refunds in your Apps with Cloud Functions for Firebase

CS373: Software Engineering Blog #7

How to Setup an Alias on Your G Suite Email?

My experience at SFPC — 3rd week

How to Recognize Audio as Text in Go

Daily Coding Problem #62

Error “com.google.common.base.VerifyException” | FlutterFix

Top 10 Bugs Found in C++ Projects in 2019

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. https://devcharmander.me/

More from Medium

Reinvent the Wheel Ep. 1: Removing Unused Files

Using assert in go tests

Apply Dependency Injection with Uber/Fx Golang