I'm Starting To Like This Configuration Pattern In Go

  Рет қаралды 18,043

Anthony GG

Anthony GG

5 ай бұрын

► Join my Discord community for free education 👉 / discord
► Exclusive Lessons, Mentorship, And Videos 👉 / anthonygg_
► Enjoy a 60% Black Friday Discount on My Golang Course 👉 fulltimegodev.com
► Learn how I became a self-taught software engineer 👉fulltimegodev.com/#mystory
► Follow me on Twitter 👉 / anthdm
► Follow me on GitHub 👉 github.com/anthdm
Functional options pattern: • The Most Efficient Str...
SUBSCRIBE OR NO MARGARITAS
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Пікірлер: 61
@anthonygg_
@anthonygg_ 5 ай бұрын
► Join my Discord community for free education 👉 discord.com/invite/Ac7CWREe58 ► Exclusive Lessons, Mentorship, And Videos 👉 www.patreon.com/anthonygg_ ► 60% OFF on my Golang course 👉 fulltimegodev.com Thanks for watching
@smhmayboudi
@smhmayboudi 5 ай бұрын
Hi Anthony GG, Thanks in advanced for your contribution on Go Community. This is the Builder Pattern. You are comparing the Config Struct and the Functional One. I did this in a project and I find out it is not well suited for big complex configs. In my case, there are separation code of multiple product with multiple service which done by directory and build tags. In addition, I have a suite test controller which separately run a test case for unit test and integration test. After a while I find some of configuration elements are missed on tests, so some of tests with the incorrect configs are passed. I understand every test should be very careful crafted by hand and mind, however there are times which we have rush. My prefare for configuration to have a basic classes which construct in main by external library (reading from yaml, toml or env). In addition, I engaged with envoy and they have a different approach which I think I should investigate. Using proto as a config manager. AGAIN MANY THANKS
@chandler828
@chandler828 5 ай бұрын
Anthony GG with another absolute banger of a video. I love these go pattern videos. I’ve implemented functional parameters at work and in my personal side project. This, in my opinion, takes it a step further. You are decoupling your service, or object, from the configuration specification. There are pros and cons to both approaches. Just another tool in your tool belt!
@cristophermoreno2290
@cristophermoreno2290 5 ай бұрын
I am starting this year strong, so. you have a new patron today ! Thank you for sharing your knowledge GG on Go.Happy new year !
@manee427
@manee427 5 ай бұрын
Happy new year Anthony and wish you all the Best!
@kushagragupta3734
@kushagragupta3734 5 ай бұрын
Happy New Year.... Great videos thanks for this.... Eagerly waiting for CAS series to be resumed.
@GushchinAndrei
@GushchinAndrei 4 ай бұрын
Thanks Antony! That’s great approach!
@whynot9963
@whynot9963 5 ай бұрын
Exactly what I needed. Awesome job as always 👏
@sweetphilly2
@sweetphilly2 5 ай бұрын
I would've thought you'd return a pointer for each "With" function
@tigranrostomyan9231
@tigranrostomyan9231 5 ай бұрын
Happy new year!!! Thank you for the video :) Also how do you manage environment variables? I feel like sometimes it can be too verbose
@yotubecreators47
@yotubecreators47 5 ай бұрын
As usual no bullshit no crap only pure 💯 advanced knowledge 💯 thank you 🙏
@tunedmystic1
@tunedmystic1 5 ай бұрын
Happy New Year! 🎉
@theRealWhexy
@theRealWhexy 2 ай бұрын
After viewing your amazing video on the functional options pattern, I changed my builder pattern to that. And now I see this... 🤯
@thesupremecoder
@thesupremecoder 5 ай бұрын
Happy new year
@digitalsparky
@digitalsparky 5 ай бұрын
I very much prefer the chaining pattern. I've used it in a number of languages, most recently PHP, and it works fantastically well there. (yes, I know, in some circles PHP is a dirty language, but as far as I see it, they're all tools for a job, not a religious ideology, if you get what I mean.)
@bertrodgers2420
@bertrodgers2420 5 ай бұрын
do you have an example of this please?
@bharadwajuday16
@bharadwajuday16 2 ай бұрын
Simply lovely...!!
@manofqwerty
@manofqwerty 4 ай бұрын
In Golang, is there a way to have a readonly prop on a struct or would you expose these using methods?
@jdawgforealz
@jdawgforealz 4 ай бұрын
Curious what this offers or using config flags/env vars? package ff is what I typically use.
@TheQxY
@TheQxY 4 ай бұрын
Interesting. Not certain if I prefer it over the functional options pattern or not. It easier to implement, but I think the functional options pattern creates a API which I think most Go users are more familiar with, so I don't necessarily think this is more clear.
@athinodorossgouromalliis9021
@athinodorossgouromalliis9021 4 ай бұрын
Nice one bringing oops patterns to golang 😅. But it is cleaner and fail safe. GG Antony
@pratiktiwari5689
@pratiktiwari5689 5 ай бұрын
Hi Anthony can you suggest some intermediate and advanced open source projects from where you learn all these things where maybe I can learn new stuff and contribute in golang
@neilcarrier1620
@neilcarrier1620 5 ай бұрын
The main difference may be the amount of control needed by users of the API: If you want the users to have control over the configuration, functional options is good, because they can write their own functions. If you want to lock down the configuration, use chaining with private member values. But always, as Anthony emphasizes, do what is most clear and simple for users and maintainers in the particular instance.
@alexIVMKD
@alexIVMKD 5 ай бұрын
Good stuff
@anyDimka
@anyDimka 5 ай бұрын
The only confusion I have here is that when I invoke function that starts with "New", I expect to receive a pointer to the structure. However, this pattern is quite popular and is widely used in the MongoDB library. Thanks for the content and Happy New Year!
@metaltyphoon
@metaltyphoon 5 ай бұрын
Agree. In C# land we tend to add the Builder() suffix on the method name to say you are getting a builder back.
@plaintext7288
@plaintext7288 5 ай бұрын
​@@metaltyphoon Adding a builder type in Go doesn't seem to be as bad of an idea. It could probably be something like: type Config struct { ListenAddr string Unspecified string } type ConfigBuilder [string] interface{} func (cb *ConfigBuilder) WithListenAddr (listenAddr string) *ConfigBuilder { cb["listenAddr"] = listenAddr return cb } func (cb ConfigBuilder) Build() *Config, { var conf Config if listenAddr, ok := cb["listenAddr"]; ok { conf.ListenAddr = listenAddr.(string) } else { conf.ListenAddr = ":3000" } if unspecified, ok := cb["unspecified"]; ok { conf.Unspecified = unspecified.(string) } else { conf.Unspecified = "foobar" } return conf } The code seems a little too much, but it guarantees that the config used has all the values initialized. Tests for the builder are a must to ensure valid types in the map Also, for testing, it will likely need util functions to initialize a basic test config which do all the builder stuff Edit: typos, formatting
@yotubecreators47
@yotubecreators47 5 ай бұрын
Anyhony GG as usual God of Golang
@zackarysantana8208
@zackarysantana8208 5 ай бұрын
Happy new years all! I think the major drawback is you are revoking the user's power to creating their own option functions, if they are reusing your func a lot and want a stable configuration function that applies what they want to the struct.
@dranon0o
@dranon0o 5 ай бұрын
I will stick to the dependency injection pattern ¯\_(ツ)_/¯ I prefer to deal with an array of typed functions Happy new year Anthony
@themichaelw
@themichaelw 5 ай бұрын
How do you test code with dependency injection? Do you find yourself mocking things a lot?
@talesmasoero
@talesmasoero 5 ай бұрын
Hey Anthony, what’s the theme that you use?
@anthonygg_
@anthonygg_ 4 ай бұрын
Gruvbox
@Laflamablanca969
@Laflamablanca969 5 ай бұрын
Is there a reason why you wouldn’t just store the config in the server and access it that way? Rather than doubling up on the fields in config and server?
@TheQxY
@TheQxY 4 ай бұрын
With this pattern it wouldn't make much of a difference I guess. But it makes it more clear which field are set by client input and which fields are inherent to the server implementation.
@mrcth6593
@mrcth6593 5 ай бұрын
good sht!!#
@crusader_
@crusader_ Ай бұрын
What font are you using?
@anthonygg_
@anthonygg_ Ай бұрын
Consolas
@thegittubaba
@thegittubaba 5 ай бұрын
It feels nice. But with each With call, we leave behind a orphaned struct to be garbage collected. That kinda irks me.
@fsra16
@fsra16 5 ай бұрын
Does using pointer receivers solve this?
@thegittubaba
@thegittubaba 5 ай бұрын
@@fsra16 That would be a slightly different pattern. WithName function becomes SetName. Then you chain multiple SetFoo calls.
@MegaMage79
@MegaMage79 5 ай бұрын
Wouldn't go usually allocate those intermediary Configs on the stack ?
@anthonygg_
@anthonygg_ 5 ай бұрын
I think its allocated on the stack to be honest. Why would it not be?
@thegittubaba
@thegittubaba 5 ай бұрын
@@anthonygg_ Yeah seems like it would actually be allocated on stack, after taking a quick look at go spec, it'll only be in heap if size doesn't fit. Also config/options are normally set only once at start of a server/big operation so I guess its even more inconsequential.
@demyk214
@demyk214 4 ай бұрын
Anthony ik weet niet of het yt. Maar doe is koptelefoon op. Geluid is slechter dan door de koptelefoons (3 getest van sony & jbl boven 200 euro) dan door de speakers ben je echt glashelder. Misschien goed om te weten man.
@pseabrook
@pseabrook 5 ай бұрын
Good video but when are you starting a dating advice channel?
@ThangTran-hi3es
@ThangTran-hi3es 5 ай бұрын
First video of 2024 right 😅😅
@vitiok78
@vitiok78 5 ай бұрын
So... If I'm not mistaken you've just reinvented the typical object-oriented approach with class that has private properties and a bunch of fluent setters. It is basics in object-oriented world
@demyk214
@demyk214 4 ай бұрын
Spouwn😂
@mitrabeast
@mitrabeast 5 ай бұрын
This is the worst... Watched because of some recommendations, unsubscribed... Why do you need to make everything more complex these days? Just use some public structure fields
@anthonygg_
@anthonygg_ 5 ай бұрын
😂
@nixoncode
@nixoncode 5 ай бұрын
sorry to see you go!
@mitrabeast
@mitrabeast 5 ай бұрын
@@nixoncode It's not that I put some value in unsubing but the fact that I'm not interested in this kind of content. Just to make sure it won't be mentioned in the comment answers. Nobody including the author gave the answer why do you actually need to complicate things. Just a haughty laughing emoji instead. Very professional response.
@perc-ai
@perc-ai 5 ай бұрын
@@mitrabeast ur just a crud developer lol learn some real skills kid
@mitrabeast
@mitrabeast 5 ай бұрын
@@perc-ai Huh? If you were an experienced developer, you could simply answer my question in a comment, without mentioning your psychological problem.
@jaymartinez311
@jaymartinez311 5 ай бұрын
Happy new year. After abandoning rust because i hated how it mutated state i’m back at golang. I was a bit frustrated at first because it was missing a lot of built in functions but i started building my own utility functions & like your video i think its making me a better programmer 🫡
Another Amazing Use Case For The FOP Pattern In Go
4:10
Anthony GG
Рет қаралды 10 М.
Why Golang's IO.READER Is More Important Than You Think
6:38
Anthony GG
Рет қаралды 11 М.
Bro be careful where you drop the ball  #learnfromkhaby  #comedy
00:19
Khaby. Lame
Рет қаралды 43 МЛН
Cat story: from hate to love! 😻 #cat #cute #kitten
00:40
Stocat
Рет қаралды 14 МЛН
Super sport🤯
00:15
Lexa_Merin
Рет қаралды 20 МЛН
The Most Efficient Struct Configuration Pattern For Golang
11:10
Official Diamond DA50 RG Demo Flight | Full Version
17:59
LifeStyle Aviation
Рет қаралды 74 М.
err != nil Is GOOD? (And Why)
7:19
ThePrimeTime
Рет қаралды 85 М.
Getting Started with HTMX and Go [HTMX Tutorial Part 1]
25:29
NoobScience
Рет қаралды 7 М.
This is Why You NEED to Use This Golang CLI Framework
15:22
The standard library now has all you need for advanced routing in Go.
13:52
HTML Templates Instead Of Reactivity | Prime Reacts
12:42
ThePrimeTime
Рет қаралды 107 М.
A Practical Example How To Use Interfaces In Golang
14:42
Anthony GG
Рет қаралды 17 М.
A Beautiful Way To Deal With ERRORS in Golang HTTP Handlers
8:42