[Golang] Interfaces and dependency injection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so today um overview of how to build interfaces and how to implement them and also how to do dependency injection in golem okay so we start with um defining an interface with the keyword interface so type the name of your interface and then the keyword interface and then you can define a bunch of signatures so this one will be get users and then speed back a string then we can implement it with a struct so default database tracked nothing to construct it and then to implement the interface we just define a instance method on this struct that we defined so then the struct name and then just the signature of your interface basically right and then we can just return a string we can run the bob for instance and let's define another one as well so let's define an agreeder for instance so another interface type greater interface and we can just define one method there so you could have grit with a string the username for instance and speed back nothing okay let's implement it so same as the database so we can implement a nice grid for instance and then the same defining the instance method implementing the interface basically then grid and then we can just log a message basically there hello then the name and then just passing the username basically all right so we have two independent components so you have the greeter and you have the database one so now let's define um the program so the program will just be the execution from the main so a program is a stroke so then it needs a database and a greeter but here i'm referencing to the interfaces so database interface and then greater interface and then um okay so let's uh instantiate a program so p equal program and then with a db and a grid so we need to provide a db and agree to so let's um give a db so the only one we define is a default database so we just build that and the grid we just have the nice critter basically it's a nice greeter so then we can pass that to the program all right let's do something with our program so um instance method on the program so a program will have the execute method it's going to get the user from the database so then program.db and then dot get user and then the creator just passing the users from the database to greet it and then we can invoke in the main program execution let's try that and there you go it's calling the logline22 from the greeter using the username we pass down by the default database bob line 12 okay so this is how you do building two interfaces two implementations and then doing dependency injection in the program basically uh all right so then um let's try to define another implementation for this database so you just define another struct so let's call it for instance famous database famous and let's implement the the interface again the method okay so we can return another name rather than bob um like that and then in the dependency ejection part so we can just swap the default database by the famous database and there you have hello with smith okay so let's modify slightly the database interface and just add another method basically get all users for instance which is uh for instance spitting back a slice of strings okay so how does it work in terms of dependency english and you can see that go is compile is a compiling error saying you cannot use the famous database anymore as database interface there you go because one method is missing basically you can't so if you just implement this signature get all users spitting back a slice of string everything should be fine all good we have with miss back so final thoughts about solid principles so you can see how our component program is respecting the dependence in version one as as a program is relying on in the interface databases and greater and not not their implementations and you could see how easy it was to swap the implementation default database in favor of the famous database this change was invisible from the point of view of the strike program about our database interface so we broke two principles so the first one was the interface segregation because we made this interface the database interface wider by adding another meta method the get all users and so immediate consequence for that was that we broke um our two only implementations default database and famous database a countermeasure for that would be to compose interfaces basically and the second one obviously was the single responsibility one where our interface um didn't have any more um one responsibility um thanks for watching happy coding
Info
Channel: Panpito
Views: 21,948
Rating: undefined out of 5
Keywords: go, golang, dependency injection, tutorial, interface, implementation, SOLID, clean code
Id: ek9XmYAPP5Y
Channel Id: undefined
Length: 8min 27sec (507 seconds)
Published: Sun Oct 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.