The Most Efficient Struct Configuration Pattern For Golang

preview_player
Показать описание

In this Golang tutorial, you'll learn about an efficient configuration pattern in Golang that will help you master complex structures and simplify your code. Discover how to unlock the power of configurable structures in Golang and create mind-blowing patterns to enhance your programming skills.
Рекомендации по теме
Комментарии
Автор

This pattern is commonly known as the "Functional Options" pattern. The Functional Options pattern is a design pattern in Go where you pass in functions that alter the state of a type. These functions are often called "option functions". They provide a way to cleanly design APIs and offer a more flexible and readable way to interact with a function or type. Nice demonstration on how to implement them. Thanks.

kafran
Автор

Functional Options Pattern... popularized by Dave Cheney

abiiranathan
Автор

1. Make the config elements private
2. Make the options type a public interface, with a single private method (apply(config) config)
3. Expose public functions returning this interface
4. Add a function type that complies with the option interface, make that type implement the apply method by calling itself.
5. Add a no-op type for validation when needed.
6. Validate input before returning a config type.

Boom, functional options with a restrictive builder pattern, for your constructors. You can make it generic too.

necroowl
Автор

Perfect timing! I am doing something right now that can use this pattern. Thanks!

jeezusjr
Автор

Reinventing the wheel of named function parameters with default values))

vitiok
Автор

It looks fancy but a bit of a hassle. A simple builder pattern is much more readable I think. The default values are nice. Then just put methods onto the ServerOpts type which return *ServerOpts.
type ServerOpts struct {...},
func NewServerOpts() *ServerOpts {...set defaults...},
func (s *ServerOpts) Id(id string) *ServerOpts {... set id ..},
func (s *ServerOpts) MaxConn(maxconn int) *ServerOpts {...set maxcon..}

Then you can use it like this:
s :=

banatibor
Автор

Thanks for diving into design patterns Anthony ! This is what separate starting devs to more advanced ones, that we all aspire to be, and that is understanding design patterns. Looks more like the Builder pattern than the Visitor one.

arieheinrich
Автор

This channel is amazing, you're making my Go code better and better for every video!

jex
Автор

Hey big boss, two questions:

1. How to make an efficient cron job scheduler from scratch?
2. How to make realtime subscriptions to database values - for example we have key value store but then we build realtime subs that can subscribe to changes of a key and its data?

Another quality video for the fans homie, love this channel.

Bests,

Super fan.

seanknowles
Автор

Quality Content <3 Thanks for sharing your knowledge, there's no better place to learn Go on YT than your channel

lokthar
Автор

This is a great video. I stumbled onto using this pattern by accident, it was very useful for a previous project I was working on

kylestubblefield
Автор

Watch this video for some time ago, remember. And return now to implement this in my project))
Antony is gigachad, thx for the video

Programscape
Автор

Thank you for posting this Anthony, very nice pattern and will be trying to incorporate it with my starter projects

skyline
Автор

Just love this man !! More like this brother, these r the stuff which really play with the Dopamine !!

bjugdbjk
Автор

The way you make any concept understand is just amazing !!

rajdama
Автор

This is amazing and used in a lot of places tbh. This is very true that it's used in gRPC, also ssh package if I am not wrong because ssh connection has a lot of options. While putting this in a library the "withXYZ" functions can also be a method where it can have the server receiver methods.

This is so so so amazing!!!

grim.reaper
Автор

Thanks, I think you also talked about a bit of functional programming in Golang in this video, which is very nice.

kanfit
Автор

Does feel like a take on the builder pattern.

Incredibly handy pattern a lot of Elm libs use as well.

JT-mrdb
Автор

only real world shit in the channel 💪🏼 love your content as always ❤

burionyt
Автор

Hi Antonio, how are you? I'm migrating from nodejs to Golang thanks to you and your videos, always amazing! I don't know if it's asking too much, but could you make a video/tutorial for developing web crawlers with golang, please? I was googling about it but I didn't find any good content about it. Thank you so much my friend, you are amazing!

pserra