Learning Golang: Functional Options / Default Configuration Values Pattern



Learning Golang: Functional Options / Default Configuration Values Pattern

Learning Golang: Functional Options / Default Configuration Values Pattern

This is tutorial for a Go pattern, similar to the Builder pattern, that allows defining configuration values, default values and validations called: Functional Options.

This Go Pattern is possible thanks to the power of Go when defining types that happen to be functions combining them with variadic arguments in the “constructor”/initializer function using those values.

I like to use this pattern when the goals are the following:

* Expressiveness, we want to clearly indicate the configuration values
* Flexibility, we can add new configuration values and our users won’t break
* Security, we can define validation rules
* Defaults, we can define values values

== Relevant Links

* Example code: https://github.com/MarioCarrion/videos/tree/2a4be54f8e98070c29baf47dffabc0dc119acf2d/2021/11/05
* Previous Episode: “Learning Golang: Versioning Tools as Dependencies using Go Modules”: https://youtu.be/g_5n0W27XcY

* Playlist “Learning Go”: https://www.youtube.com/playlist?list=PL7yAAGMOat_F7bOImcjx4ZnCtfyNEqzCy
* Playlist “Software Architecture in Go”: https://www.youtube.com/playlist?list=PL7yAAGMOat_GCd12Lrv_evJ3Zhv1dl8B-
* Playlist “Learning Go/Golang Concurrency Patterns” https://www.youtube.com/playlist?list=PL7yAAGMOat_Fhj_px_DzNzTsXs-mRwv1t
* Playlist “Building Microservices in Go”: https://www.youtube.com/playlist?list=PL7yAAGMOat_Fn8sAXIk0WyBfK_sT1pohu
* Playlist “GoTools and Packages”: https://www.youtube.com/playlist?list=PL7yAAGMOat_HEEOvH99agDs_5g51A0Ls3
* Playlist “Testing in Go”: https://www.youtube.com/playlist?list=PL7yAAGMOat_HSeW4zF0uRL9EaHadE4ZZq

== Socials

* https://twitter.com/MarioCarrion
* https://www.instagram.com/mario.carrion
* https://linkedin.com/in/MarioCarrion

00:00 Introduction to Golang Function Options / Default Values
04:03 Extract arguments as a new type
06:46 Adding Functional Options
13:33 Adding Validation and errors
17:40 Adding Default Values
19:20 Conclusion

Who am I:

HelloπŸ‘‹πŸΌ! I’m Mario, a Hands-on Software Architect and Lead Backend Engineer with more than 16 years of professional experience building all kinds of software including on-premise Industrial Automation Systems, Linux Accessibility Desktop and Browser Components as well as Distributed Advertising Microservices.

Every week I will share with you different topics I’ve learned while working for small startups and large companies including the processes I’ve followed over the years for successfully delivering complex enterprise systems from start to end.

Subscribe if you like Software Development, Software Architecture and Systems Design!

Keep it up. Don’t give up!

#golang #designpatterns #programming

— Our affiliate links below

Shop our Amazon favorites β†’ https://www.amazon.com/shop/rubycarrion
Shop my IG feed on my LIKEtoKNOWit (LTK) page β†’ https://www.liketoknow.it/RubyCarrion
Shop Top Deals and Featured Offers at Best Buy β†’ https://bestbuy.7tiv.net/c/2558226/687081/10014
Get a 30 day FREE Trial of Epidemic Sound β†’ https://www.epidemicsound.com/referral/szx441/
Try Amazon Prime 30-day FREE Trial β†’ https://amzn.to/3yf9a0f
I love getting Cash Back and think you will too! Join for FREE and get $30 when you spend $30 β†’ https://www.rakuten.com/r/RUBYRA132?eeid=28187

— Our Vlog Channel

https://www.youtube.com/c/RubyCarrion/videos

Comments are closed.