Options Pattern in Golang

While browsing one of the open-source repositories, I came across such pieces of code quite a lot.Focus on the ToServerOption and its use. I tried to understand it for a fair bit amount of time but was unable to understand why this was needed. I unders…


This content originally appeared on Level Up Coding - Medium and was authored by Himanshu pandey

While browsing one of the open-source repositories, I came across such pieces of code quite a lot.

Focus on the ToServerOption and its use. I tried to understand it for a fair bit amount of time but was unable to understand why this was needed. I understood all the code that was Infront of me but was unable to piece things together.

Until I read this line in one of the issues.

Suddenly I had a eureka moment. I knew that it was referring to that particular piece of code that I kept seeing. It reignited my curiosity since I had a clear direction to follow, i.e. “Options pattern”, so I started to research it a bit more.

So, What is “Options Pattern”?

Option pattern is a functional programming pattern that is used to provide optional arguments to a function that can be used to modify its behavior.

If you come from a python background you might be familiar with this.

The client can choose to skip this lastName parameter if it is not needed or use it when it is.

This ability is very useful in API design as this

  • Allows clients to use your SDK using minimum configuration while still providing ample configuration options for experienced users.
  • Allows you to add new options without breaking backwards compatibility.

In Golang however, this is not possible. The language does not provide a way to add optional parameters.

That’s where “Option pattern” comes in. It allows your client to pass additional options while calling your SDK, which then, can modify its behavior accordingly. Let’s look at a small example.

Suppose we have a struct named Server which has 3 properties port, timeout and maxConnections

Our factory method will look something like this.

But I wish timeout and maxConnections to be optional arguments. In many cases, the default value of these is sufficient. Also, I don’t want to bombard new users who are just learning or experimenting with my SDK with these unnecessary configurations. Let’s see how we can achieve this.

First, let’s define a new type called Option

Option is a function that takes the pointer to our Server . This is important as we will be modifying our server instance with these options.

Now let’s define our options. Things will become clear. The convention is to prefix our options with a With but feel free to choose whatever name fits your domain language

Our two options, WithTimeout and WithMaxConnections, take the configuration value and return an Option . The Option is just a function that takes a pointer to our server object and sets the desired property to the provided value. For example, WithTimeout takes the timeout duration and then returns a function (whose signature is the same as Option) that sets the timeout property of our server to the value that was provided.

Here we are utilizing a technique called a “Closure” that almost all modern languages(including Golang) support.

Our factory method for Server now needs to be modified to support this change.

If you want, you can add port as an option too.

Here you can see that our client can now create a minimal server with just port, but is also free to give more configuration options if needed.

You can look at this YouTube tutorial that I referred to for better understanding.

This design is highly scalable and maintainable, even more so than the optional parameter we see in python. It allows us to add more options without bloating our function signature and without touching our code in the factory method.

A Small Tangent

This incident that occurred to me shows the power of names and why they are needed. When I didn’t know the name of this pattern, I struggled to understand what this piece of code was doing or why it was needed. Every time I saw this code, my brain had an overhead of parsing it. But as soon as I learned its name, I instinctively understood this code. Now when I see this in the codebase, I just know what it is doing, what its purpose is and the code is much easier to parse.

Resources:


Options Pattern in Golang was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Himanshu pandey


Print Share Comment Cite Upload Translate Updates
APA

Himanshu pandey | Sciencx (2022-03-09T23:30:23+00:00) Options Pattern in Golang. Retrieved from https://www.scien.cx/2022/03/09/options-pattern-in-golang/

MLA
" » Options Pattern in Golang." Himanshu pandey | Sciencx - Wednesday March 9, 2022, https://www.scien.cx/2022/03/09/options-pattern-in-golang/
HARVARD
Himanshu pandey | Sciencx Wednesday March 9, 2022 » Options Pattern in Golang., viewed ,<https://www.scien.cx/2022/03/09/options-pattern-in-golang/>
VANCOUVER
Himanshu pandey | Sciencx - » Options Pattern in Golang. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/09/options-pattern-in-golang/
CHICAGO
" » Options Pattern in Golang." Himanshu pandey | Sciencx - Accessed . https://www.scien.cx/2022/03/09/options-pattern-in-golang/
IEEE
" » Options Pattern in Golang." Himanshu pandey | Sciencx [Online]. Available: https://www.scien.cx/2022/03/09/options-pattern-in-golang/. [Accessed: ]
rf:citation
» Options Pattern in Golang | Himanshu pandey | Sciencx | https://www.scien.cx/2022/03/09/options-pattern-in-golang/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.