Mastering Dependency Injection In Golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to do a deep dive into dependency injection in goal line a lot of people are commenting on my videos and they basically Cry for Help they say Anthony I wish golang had a proper way of injecting dependencies Anthony I wish Golan had had a dependency injection container I don't even know what a dependency injection container is I disagree I think golang has one of the best mechanics for dependency injection out there before we do a deep dive if you're not subscribed to the channel and you like the content I'm providing to you please consider subscribing give me a thumbs up leave some questions in the comments jump into my Discord Community right and for the people that really want to level up I made the full-time godf program specifically for people that are willing to become professionally active as a goaling engineer in the industry we basically covered everything from the basic mechanics of cooling mastering concurrency building Json apis microservices you even have my treat to you a free blockchain from scratch more of a distributed uh lesson and of course we're going to cover how to land a job looking forward to see you as one of my students okay before guys before we uh can actually take a look at dependency injection we first need to know what is a dependency right what is the dependency how can we spot dependency in our application so we're going to do that real quick so we're going to say type uh Rock climate because we're going to make uh some kind of rock climbing game or or application right so it's going to be a structure we're going to say a rocks client uh client is going to be an integer and we're going to make a function heat and we're going to say RC from a rock climate it's going to be a pointer because we are going to adjust the Rocks climb so that's why it's a pointer and we are going to see a client Rock right and what the only thing we're going to do is we're going to say RC rocks client plus plus easy peasy but we know that if we Are Climbing rocks we're going high up into the sky so what we need to do is that maybe after a couple of meters we need to basically play some safety cards we need to prevent that we are going to smack our cheeks down onto the Glo onto the ground after falling down 100 meters because that dependency injection doesn't really matter anymore so in this case we're going to make something very simple we're going to say FRC rocks client if RC Rock's client is going to be 10 we could actually make it a module uh so you can basically this function is going to work forever but in this case we're going to be bad programmers and we're going to say rocks climbed S10 and each time uh if we have 10 rocks climbed then we're going to say RC Place safeties uh safeties something like this right so after 10 rocks we're going to grab or uh tooling and we are going to hammerce down a safety into the ball right so we're going to make this function heat we're going to say again RC pointer to the Rock climate we're going to say Place safeties and that's going to be an fmt parental n and we're going to say he's placing my safeties something like that right nothing really wrong with this thing uh so we're going to say RC is going to be this uh rock climbers yes just like that now we're gonna loopy real quick let's say we're gonna Loop 11 times and I'm gonna say RC uh Kalyan Brockie boom butts go run dots and replacing safeties all fine so um nothing wrong with this but we can see that there are going to be some problems further on because the climbing rocks and um maybe we Are Climbing different kinds of rocks right maybe we Are Climbing for example ice rocks Sandy rocks uh maybe you're even climbing concrete or something I don't know right so you can already see that there is going to be an effect on our place safety's function right here right there's not going to be a pure function anymore it's going to be a function that's going to be heavily depending on the type of rock we are going to climb so that's no big of a problem we can modify that you could say and teach kind and right you could say uh switch RC that kind right and we could say case one which maybe is ice but we could say case two which could be sent and case three which could be for example concrete right no big of a deal that's fine right switch statement why not but placing safeties can be very very Heavenly have a heavy function and what do I mean by a heavy function is that maybe placing safeties is going to go to our database maybe placing safeties is going to go to a third-party API uh maybe it's only for ice rocks we're going to go to the database because we're going to check what kind of temperature temperature uh ice is going to have or something and then based on that we need to do a third party API or something or actually we're going to first go to the third party API to check the temperature with the sensor and then we are going to go to the database to pick the right tools for that type of ice right so you can see that placing safeties can have big effects on a rock climber which basically means that placing safeties is a hard dependency so what people will do is set okay no problem we're going to extract placing safeties out of the domain of our climber right this entity the domain of our climate we're going to extract placing safeties out and we are going to make something like a type a safety place a safety Placer actually to be honest safety safety Placer is going to be a structure right this can have a kind no problem and and then we actually going to basically completely copy this function paste it in each and we're going to say SP from safety Place search which is going to be safety placed right and we're going to see SP kind and we're going to do the same thing right so uh what we need to do basically is saying we extracted the place safety from the rock climber so what's going to happen is now we're going to say that the SP which is the safety Placer right the rock climber is going to depend on the safety places that's something we can agree upon so what's going to happen is that instead of saying RC after we climb 10 rocks we're going to see RC dot SP dot Place safeties this is already a better design because right now the rock climate the function the logic of the rock climate is now at the domain of the the rock climber domain doesn't has the logic of placing safeties because it's in the safety places but actually you're just moving the problem somewhere else you'd not solve the problem you're moving the problem your rock climber is going to depend still on the safety places although your logic is not in the Rock climate anymore but it's in a safety places that's bad design because you have a hard-coded dependency heat right it's basically the false Assumption of being a good programmer because you think oh yeah I split it out now the other climber we have a safety Placer just put the safety Place into the Rock climate you beat the poopy but that's not true you're basically just uh depending on the same thing but different right so how can we solve that right well if you want to do dependency injection in Golden which is a very simple concept that's getting overly complicated right is we're gonna make the safety places we're gonna make that we're gonna put that on top and we're gonna actually say that the safety place that is not a strict but an interface right and the only thing is safety place is going to do it's going to do Place safeties uh safeties like this right that's the only thing or safety place is going to do that's the only thing we care about right so instead of uh this thing we're gonna quickly comment that real uh out real quick like that and wait do we have this is going to be capitalized I'm going to make the small caps by the way uh so it's going to work right so now we're going to see that this rocks Calamity still has a dependency right the Rocks climber still has a dependency it's still depending on the safety Placer but it does not depend on the implementation of the safety places it implements on a behavior of the center of the safety places and that's completely different the implementation is in my if it if your program depends on an implementation you have a hard-coded dependency if your program depends on an Behavior you are basically a God because a behavior can be anything so you can jiggle around with it right because can you imagine we are basically depending on the implementation of a safety place that hard-coded like our previous example and we need to test even though we don't want to test a rocks climber on placing the safeties we still need to uh inject we still need to constrict or Rox climber with the safety place to be implementation which could have a database so we need to have a mongodb setup it's going to do some API calls so we need to have an API key and and some URLs and all that stuff so it's nasty even though we don't want to test it right but now we are depending on a behavior we are free to go right and I'm going to show you right so basically uh of course not gonna work right now because we don't have an implementation so we're going to make that right so we're going to say type uh we're gonna say ice safety uh safety place or something like that which is going to be a structure right and this eye safety place it can hold the DB it can which is also a dependency right um that we need to inject it can hold some other stuff right API keys for for some Shenanigans uh and then we're going to say heat uh wait how can I what's going on like that's perfectly fine I'm gonna say safety Placer which is going to be the eye safety place we're going to delete this whole Bank need and we're gonna say placing my eyes safeties right perfectly fine so uh let's make a Constructor here Funk new rock climate which will take in Sp we're gonna inject we're gonna enjoy this is dependency injection we're going to inject our safety place it right which is the behavior not the input well we're going to inject some kind of implementation but it depends heat on a behavior I hope that makes sense it's very important aspect to understand it's going to be our climate pointer and we are going to a return Niche um the rock climber of course and we're going to say that the SP is going to be the uh Safety Research interface right so uh we're going to say he or C New uh Rock climate right and we're going to say the eye safety Placer in our case right if you run this did I save this yes if we run this now we're going to place my eye safeties perfectly fine but maybe with all this a bank it's doing API calls it's doing a database uh queries and all that stuff heavy dependency but now I want to have a a not so heavy dependency because I'm testing my code on something else so what I'm going to do is I'm going to make for example um this is that I'm gonna do this think um no type nope nope stands for no operation uh safety places for example it's going to be a structure just like that and then we're gonna copy the save play save this function here just like this call this the knob perfectly fine and uh we don't do anything here we just maybe print out we're not going to do the database it's just nothing it's just it's a placeholder it's a mock it's a mock that's maybe a better thing placing no safeties at all you know what I mean so uh instead of doing this eye safety playset we can actually say the knob safety places right no big of a deal we just switched or we just injected a new dependency with the blink of an eye without modifying anything in the Rock's climate right we didn't touch them at all we only changed the implementation of the safety Placer Behavior right so let's go run this real quick just to show you to get some dopamine going right placing no safeties because we're testing no DB call no API call no not then good to go this is basically dependency injection right there's nothing more to it right because dependency injection injection is just basically um instantiating some kind of implementation into another object into another entity which going to depend on that object right it cannot live without that object that's the dependency you know what I mean so how can we like I said before how can we make that even better is by not depending on an implementation but depending on a behavior which basically is being done with interfaces going we are depending on an interface which is the place safety's Behavior which can be any implementation we do not care about but that's dependency injection in goaling it's easy it's simple it's powerful there is no need to make it more complex than that right if you like this video consider subscribing to my channel give me a thumbs up leave some questions in the comments and again if you want to be professionally active as a goaling engineer these kinds of Concepts this kind of stuff it's what I'm doing in the fulltime godad.com program all the time right if you want to be a very specialized cooling engineer check out fulltimegoodev.com and I'm looking forward to see you in one of my videos or live streams thanks for watching bye
Info
Channel: Anthony GG
Views: 27,990
Rating: undefined out of 5
Keywords: golang, dependency injection, go programming language, golang dependency management, golang dependency injection, golang tutorial for beginners, golang tutorial, golang for beginners, golang programming, learn golang, dependency injection simple explanation, dependency injection simple example, golang programming language, go language, programming, golang course, golang beginner tutorial, go programming language tutorial, golang introduction, go lang, go tutorial
Id: UX4XjxWcDB4
Channel Id: undefined
Length: 14min 29sec (869 seconds)
Published: Tue May 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.