The Power Of Golang's Decorator Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up boys and girls in this video I'm going to show you a very very important pattern technique um that basically uh implies to all programming languages but in golang it's it's a very important pattern I'm using all the time uh it's basically uh make a function accept and do whatever you wanted to do pattern right also called the decorated pattern or maybe the middleware pattern it's a very important aspect and I swear to God this um I'm using this all the time I'm even teach this in the full-time godf program which is by the way guys for the people that don't know for the people that are willing to become a full-time goaling engineer I'm making a complete program from A to Z all batteries included the complete cooling syntax concurrency uh we're even going to build two insane projects full real world examples the Json API is going to be in hotel reservation backend and we're also going to build a complete microservice stack which includes multiple services in the server communication gateways rate limited grafana Prometheus Kafka the whole shebang is included check that out if you uh are interested alright without any further Ado let's do this right so basically first of all uh we're going to make two simple functions that mimic dirt Port third party uh Library differentials right because um that is where the problems with sites right most of the time these third-party stuff they come up with interfaces they come up with functions and you cannot modify them right because they they come from a third-party library right so the first thing we're going to do here is uh let's make our friend coming real quickly right maybe boost up um the font size a little bit for the blind homies so we have this one coming let's make a function and this function is going to be um yeah let's call this an execute think right an execute function this is coming from a third-party Library very important to understand right and this is going to take in an event and that's going to be an execute uh event right and execute advantage and we're going to return nothing right and what this function is going to do is basically going to take in this event function and it's going to let's make this interface real quick we're going to say um not an interface we're going to make this a typed function I'm going to say execute uh a van right which is going to take a string heat and it's going to actually if you want to if you type your finishes you don't need to provide these things and that's the only thing it's going to do right type execute event and we are going to execute each and um we're gonna a simple string for example right something simple right just like that uh cannot call non-function um of course because I didn't do this boom now we're good right so basically this uh is coming from a third party lip right very important to understand right so we have this stuff it's all good and normally what we should do is something like this what we call this execute and let's make a function here on top we're gonna say thank my AXA cute funky and um it's gonna take in a string which is this and we're gonna say um for example uh this stream right so basically that's the only thing we're gonna do right so let's in main heat we're going to see my execute Funk call is a day open this up and do a goal dot boom and you see it's printing out fubot bus right because it's coming from uh my ex Funk right this thing you see up do it again actually to be honest boo my x-trick right simple right this is this is default stuff this is where basically uh the interesting part comes right and it's a very guys I swear to God this is a very very important pattern right um so basically but okay let's say we cannot modify these functions here right uh but we want to inject this is also some kind of dependency injection technique right very important because we want to inject our database into uh here right we want to we want to have access to database access to DB right but how do we do that well we can make it a global a global thing but that's hey that's bad because how we gonna test this with a global DB that's completely whack right nobody's doing this so basically let's say we have for example an uh DB um yeah and it's going to be an interface right interface and this DB is going to have a stored function and it's going to take in a string and maybe a certainly an adult or something I don't know and then we're gonna make our own DB real quick um that's going to be a type that's called a storage which is going to be a structure with nothing attached no value is attached to it I'm going to say funk as pointer storage can I type point to store and we're going to see uh of course it's going to be stored heat you're going to take in a string and that's going to return an errors right and we're going to say fmt printerland um storing and 2db this s right boom of course we determine alert we're going to return no because everything is fine we have a problem here because we have duplicated s that's no problem we're going to call this a value just like this uh value perfectly fine right so how can we actually do this well we can do the what I call the make a function do whatever the you want it to do and accept uh that you wanted to accept The Decorator pattern so what we're going to do is is this my execute hook instead of saying it's going to take a string we're going to say no that's not going to happen we're going to take in a DB which is not a storage that would be BS we're gonna make this the DB an interface right very important and instead of returning nothing we're gonna return this execute think this execute FM which is coming from the third party library right so then we're gonna return it right but we're gonna return you're gonna return a function signature from this execute thing right so uh that's going to be return a function which is going to take in a string heat right and it's going to return nothing so we're going to copy this thing and to heat and now if I Center this what we can do here what's going on yeah what we can do here is basically we could say DB storage right uh can we please yes boom just like that so uh that's what we want right we want to have our DB that's what we did here but look at this we injected rdb and through this function we the previous version we could not we could not access it now we can of course this is going to be a problem here because it's telling us cannot use my execute func value of type yada yada because uh it's not the correct type of course not we need to actually do this right and then we're going to say that rdb to be honest you could do it like this let's make it we're going to say storage or S right it's going to be an and Storage just like this pop-up and then we can actually say any act this storage here which could be anything right because it's an interface right now it's a store it doesn't really matter because it's an interface which basically means it could be anything that we want it to be and if it implemented this stored method right or a subset or or multiple of these interface methods but for now there is one right so if we run this function right now uh go around dot you see that we are executing uh our function and we're also storing it into the DB hey this looks like good to me isn't it right so uh this is basically uh what I call the decorated pattern right uh and I teach this all all over the place right I'm using this all over the place um it's a very very interesting pattern that is actually it's not that used that much I I do not see this uh at very often but it's such a powerful pattern and if you know this in the beginning is it a little bit hard to grasp I can completely understand that but um it's so powerful and you can do way more than these things especially for the people that are interested like I said before fulltime godav.com my program we are using this all over the place right we are modifying we are doing our logging metrics we we wrap like an onion right like an onion and we wrap things on wrap things and wrap things uh so we don't we have a beautiful dependency injection without pain that's what it is and what could be another good use case before that's already the eight minutes in the video which is basically means that 90 is already piecing out but it's for the people that are stick that are sticking I have some good news for example heat in our main right we're gonna have uh here for example an uh HTTP Handler handle Funk right for HTTP stuff can we please yes and we're going to say slash heat uh the root for example and we're gonna say handle it right so basically if you're going to make a handle let's make it heat a funk handle it which is going to take in a w HTTP response uh write it like this a pointer to an http.ed request right and yeah that's it so now everything is fine the problem is where is my DB right here again where is my DB nowhere to be found right of course in a Handler you want to have DB right because otherwise it's it's going to be a very boring application that does not save states right which could be the case but most of the time it doesn't the same thing right you just do this right you're gonna say make for example uh HTTP for example like that and we're gonna say it's gonna be a DB which is our DB interface heat and it's going to return HTTP Handler Funk right uh are we gonna return basically uh this thing is we're gonna copy this paste that in uh just like that right and then we could do something like uh DB storage uh some HTTP shenanigans negan's boom delete is Handler and then we could say heat uh let's paste it below our store here Center the screen this is going to be make a make HTTP Funk with the stored inside of it say that you have a problem uh uh return of course we need to return a function here guys right Boop save it make HTTP make boom and now we have this beautiful uh program that will compile right it's going to compile perfectly fine boom you see this make HTTP from here of course you want to have multiple handlers well uh you could do something like this type HTTP um Funk which is going to be a funk of W HTTP response um writer right or http a requests something like this um no that's actually bad we're gonna do this funk DB DB right that's what it's going to be and it's going to return HTTP Handler funk yeah beautiful I think something like that um is that true no yes no no no that's what we're gonna do we're going to return a narrowly guys this is on the Fly I didn't plan this for this video but it is what it is sometimes so we're gonna say make HTTP Funk uh what we gonna do here is uh we're gonna say DB maybe we're gonna give this an HTTP phone cage why is it basically doing this it's so weird event right and I'm going to say here then DB of course we need to actually have access to um yeah yeah I'm gonna make this good guys no worries http response writers requests HTTP request I'm so sorry guys uh it's going to return an error let's don't don't do not do that actually to be honest doesn't matter you can do whatever you want um so now we're gonna say not enough arguments that's true because we're going to say wrheed right you could even say f Eros as this function or function error is not nil right you could do some stuff here if you want and then of course heesh we need to inject uh a Handler right and now we can say funk handle it let's join everything inside of these brackets can we paste that in perfectly fine Eros screen return nil Heat boom not enough arguments I know that so we're going to say s and I'm gonna say heat handle it boom and you're done right so you're basically completely modified uh the Handler of HTTP and you can do a lot of more neat tricks right you could do for example like a a context or something where you put these things in so you don't have as much arguments but you can the possibilities are endless right you are The Barbers of your painting you can do whatever you want I hope this video uh teaches you something and I say it again uh if you like this video please consider subscribing give me a thumbs up leave some questions in the comments jump into my Discord community and for the people that really want to be become a full-time goaling engineer or just want to become um very very good at it uh ready to enter the in the the professional industry check out fulltime godef.com and I'm looking forward to see you as a student in my program cheers
Info
Channel: Anthony GG
Views: 43,576
Rating: undefined out of 5
Keywords: golang, design patterns, golang tutorial, go tutorial, software development, go programming, golang for beginners, go programming language, go programming tutorial, golang tutorial for beginners, learn golang, go tutorial for beginners, golang programming, golang course, go programming language tutorial, golang tutorials, golang beginner tutorial, introduction to golang, golang introduction, learn go programming language, go, go lang, go for beginners, learn go, go language
Id: GipAZwKFgoA
Channel Id: undefined
Length: 14min 8sec (848 seconds)
Published: Sat Apr 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.