The Most Efficient Struct Configuration Pattern For Golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what up boys and girls in this video I'm going to teach you I'm going to show you a golang pattern that will legit blow your mind but before we continue if you're not yet subscribed to the channel because 50 of my viewers are not yet subscribed please consider subscribing it you will do me an enormous favorite leave some questions in the comments and jump into my Discord community and for the people that really want to level up I made the full-time godf program specifically made for people that willing to become professionally active in the industry we basically covered the whole shebang introduction concurrency apis microservices you even have a blockchain from scratch as a my treat to you and how to land the job check it out 30 off uh what's included is all on this website fulltime goldf.com see you there all right so basically this pattern like I said is going to be a legit insane pattern right but before I can actually explain it to you we first need to see the root cause of the problem why do we need this pattern why can't this pattern be handy right so for example uh we're gonna have a server read right and it's going to be a structure and the server is going to get um is going to have some options right so for example we're going to have a Max connection right it's going to be an integer we can have an ID for example it's going to be a string and for example TLS right it's going to be able of course there's going to be multiple uh configuration uh variables right but this let's start with three so what would you do uh you will make a for example uh a new server Heat and you will say that the max connection is going to be an integer integer the ID is going to be a string and the TLs is going to be a Boolean right I'm going to return a pointer to the server here we're gonna return uh hands are so cold not gonna lie we're going to return the survey heat and we're going to see uh maxcom it's gonna be the maxcom right the ID is going to be the ID and of course the TLs is going to be the DLS right you can see the more options you have the bigger your new server is going to be uh this this function signature the more arguments is going to take and if you want to construct one you could say new servers right and then you're going to say for example XCOM is one the ID is full and the TLs is going to be um false something like that not bad but the problem is you can see it if you make a library or something and your server is going to have multiple or more configuration options it's going to get very tedious right it's going to get very big so what a lot of people do is this right they say okay no problem we make an opt or a config structure right and we are going to move these uh variables inside of this up spat and this obstruct right and I'm going to say we're going to embed this up thingy right here and instead of basically doing all this we gonna say instead of multiple arguments we're gonna take one simple argument which is the Ops structure right and then we say the Ops is the Ops and to construct that you need to construct it you need to provide an Ops strict so what we're going to say is going to be an opt and then we can actually basically leave it blank and you can already see the problem if people don't want to configurate it or they just want to set the TLs you need to have a way to basically um check your Ops and your new server right and you need to make sure that you have this default configuration and all that stuff but what if you want to provide a library or a module where users don't specifically need to provide options but give them the possibility to do it if they want so then you need to do some fancy stuff some Witchcraft and some voodoo unless you use this pattern I'm going to show you the first thing we're gonna do is we're gonna make a type which is going to be an opt thunk which is going to be a funk that will take in a pointer to Ops I but then we are also going to make a function and that's going to be called default Ops which is going to be a function that will return this option right it's going to be our default options if nobody is going to provide our options we're going to have this default stuff going so we're going to say return and Ops I'm going to say for example at the maxcom let's say 10 the ID is going to be uh default uh default like this yeah and then the TLs is going to be false for example right so the first thing we're going to do in our new server read as we're going to say that o from Ops is going to be default opt that's the first thing right and then what we're going to do instead of uh that the server is not going to take the opts it's going to take this it's going to take a slice and a leftist slice of opt Funk right it's basically a slice of opt Funk and why do we do these three dots here I think it's going to is called an ellipsis or something I'm not quite sure don't shoot me in the footage the reason why we do this is this gives us the possibility to give no options one option or unlimited options so it's a very genetic it's very cool it's very nice for users that are going to use your library or module it's very yeah they can do what they want right so of course we're going to say that the options is going to be this oh which is going to be the default options but after we set the default options we are going to range uh we're going to range over the opposite like that and then we are going it's going to be basically an event right this is this function it's the opt Funk here right it's basically somewhat of the pattern I'm I teached you uh the previous video it's not the same but we are using the same mechanics right it's it's like I said this pattern is very very very useful the previous video and we're going to build upon that so what you're going to say here is for each of these options if there are one we are going to call this function on the already default options but we need a pointer so we're going to take the reference right so what does that mean if nobody provides us options we have the default options and if there are some opt functions we can basically um modify the options pointers so how does this work so for example uh you want to have a function and this function is going to call with DLS right and what this is going to do is going to take this off seat which is a pointer to Ops and we're just going to say ops.tls equals true right boom and then let's print out the server real quick let's do a print F here percentage plus v so we can see it in depth s right if we don't specify any options and we do a golden dot we're going to see that we have a Max gun of 10 ID is default and the TLs is false but now we want to give the user the option to say Okay I want to have a server but with DLS right if you do this again go round up then we see if I save this uh did I make a mistake each uh no go run a DOT now we can see that the TLs is true right but maybe we want to have an other configuration Funk which can be uh Funk with uh Max con right and this is a little bit tricky because now we're gonna say here uh we want to say with Max com and which is going to be the max connections instead of returning uh we instead of returning not in heat we need to return an option right which is basically you're going to say return funk um opts pointer to Ops can I please type yeah I'm gonna say this real quick and then we're going to say heat that the ops.excom is going to be this n right that we provided so now if we go back to our server we can say with TLS with maxcom then let's do 99 because we already have them right let's say this a couple times uh go run dot now we can see that the max con is 99 and the TLs is true but we don't need to provide TLS we can say just max gone all right if you run this again you see Mexico 99 and now the TLs is false so this is actually if you check if you think about it a very nice pattern for config for configurating your structures uh specifically um for Library specifically if you want to make something for your team or something and you give your your colleagues or people in the open source Community the option to configure it like that believe it belongings or whatever you're going to say that right and if you really want you could say also with ID for example funk with ID which is going to take any string it's the same thing we're going to return this option key right and we're going to say a return funk Ops pointer to Ops like this and then we're gonna say Ops dot um ID is going to be S right actually to be honest let's call this ID he's a little bit better just like that so now we're gonna say I want a new service with maxcom but I also want a new server with ID custom ID heat and it's going to be but Foo bus save this and run this again and now we have our custom ID right and if we don't specify anything um it just works fine and we have the default options hey isn't that amazing let us be honest how this pattern is being called I have no clue uh let's call it the decorated with strict configurating pattern I don't know uh it's just something that is used it's basically not known by a lot of people but it's getting used by for example grpcs using this pattern I'm using this pattern in uh the Hollywood's active framework for configurating your actors and I think is there something else I can think about not quite Church uh like I said a very powerful pattern specifically if you want to make the life of people much easier well that's it if you like this video consider subscribing to the channel jump into my Discord Community leave a comment uh thumbs up all that Shenanigans and for the people that are willing to learn all these stuff check out the full-time godav.com program 30 off and I see you in my live streams or video or in the program or even in the Discord right bye
Info
Channel: Anthony GG
Views: 65,107
Rating: undefined out of 5
Keywords: golang, learn golang, golang tutorial, go lang, golang for beginners, go programming, go programming language, learn go, go programming tutorial, golang course, golang for begginers, go language, backend developer, golang language, go tutorial, golang tutorials, golang struct, golang introduction, why learn golang, golang configuration, introduction to golang, go tutorial for beginners, programming, go programming language tutorial, design patterns, software development
Id: MDy7JQN5MN4
Channel Id: undefined
Length: 11min 9sec (669 seconds)
Published: Thu May 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.