Master Golang with Interfaces

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everybody and welcome back to another video so in this video you should prepare to level up because in this video we're going to go over one of the main things that separates a beginner go developer from an experienced go developer and that is interfaces so understanding interfaces is going to put you well above the competition when it comes to reading and writing code using the go programming language and after watching this video I'm sure that you'll understand interfaces in the go programming language which just trust me but before we get into the explanation for those of you that are interested in becoming an indispensable member of a goang team I have a full course available on microservice architectures using the go programming language where we'll build out an entire microservice architecture and following the completion of that course you will also get career coaching from me personally so if that's something that you're interested in the link will be in the description below but without further Ado let's jump right into the explanation of interfaces all right so interfaces so most people explain interfaces using polymorphism as an example but I actually don't think that that's a good way to explain it because you end up explaining polymorphism but not necessarily explaining interfaces so I'm going to try and provide an easier way for you to understand interfaces so let's imagine that we have an application here and it doesn't matter what this application does or what it it's for all we need to know right now for this explanation is that this application needs to connect to some form of database so if we have DB here there are multiple different types of databases right so we have our standard like relational databases so maybe we have like postgress MySQL and we even have our nosql databases like mongodb and I think you get the point like there are multiple different ways or different technologies that we can use used to persist data right so if we're structuring our application in a way that's modular and we're separating the responsibilities of our application into individual components it would make sense that the database logic would be abstracted away from the application logic so instead of building all of this database logic into the application like tightly coupling it with the application we can have our application provide some interface now I'm going to start using the word interface but I'm going to explain to you what it is so let's imagine this is our interface now let's take a second to think about what an interface is like what is an interface just outside of the context of programming what is an interface like the dictionary definition of an interface is a point where two systems subjects organizations Etc meet and interact so it's a point where two things the application and the database implementation interact like meet and interact act so if we think about it from a physical perspective I just want to use an example here because this is like the simplest way to understand what an interfaces so there's a thing called an audio interface and it looks like this and this is a piece of hardware and this Hardware allows for the input an output of audio signals to and from a host computer or recording device now just bear with me this this sounds wild but this is going to become clear soon so let's imagine that our database is the recording device and our application is the computer and the audio interface is the interface here so in order for us to be able to for example record sound from the audio device and send that signal to our computer we need to be able to plug into the interface right and that interface in turn plugs into the computer and that means that we can send the sound from our audio device to our computer in order for this to work the audio device has to have a plug that conforms to the shape of the interface so that is the audio interface has these ports right and they have a specific shape to them and only plugs that can fit into that shape can interface with our audio interface so that means that the audio devices need to conform to the contract of the interface now the same concept applies in programming but in programming it's a bit more difficult to to grasp cuz we're not talking about like physical objects we're talking about like virtual things but it's the same concept so what does that mean that means that our DB implementations they need to implement our interface that is their plugs need to be shaped such that they can fit into our interface or fit into our Port so in code or in the virtual world what that might look like is this this interface maybe defines some methods that need to be implemented so maybe we need close method to close the database and maybe a select user method to select a user so we can imagine this is our interface definition which is essentially the same thing in the physical world as the shape of the port where we want to plug into right so this is defining the shape of the port so if all of these different database Solutions provide the implementation details for closing their connection and for selecting a user from their tables then that means that their plug is the same shape as the port provided by the interface like these are conforming to the interface so we can plug into the interface using either one of these and from the application perspective the application doesn't need to know the implementation details like the application doesn't need to know how postgress is implementing the close method or the select user method because from the application perspective all it cares about is whether or not it can call this close method and whether or not it can call the select user method and the interface is going to have like the function signature definition as well so select user is probably going to need a user ID passed in as a parameter right so that means that the interface will have that in its definition so I don't have space so I'll just put here that user ID is a parameter so that means that all of these different database solutions they need to provide the implementation for a method where you can select a user using user ID and this is how these different database Solutions can plug into our application so the interface is exactly that if you want to think about it from a physical standpoint it's a port the same as with the audio interface so it's like a shape and in order for one of these database solutions to plug into our interface it needs to conform to the shape of the port right the plug needs to conform to the shape of the port in the way that it does that is by implementing the interface or conforming to the contract of this interface and that opens up a number of opportunities for us so that means that we could do different things like maybe there's some business need to swap out one database solution for another one since the application doesn't care about the implementation details of the database as long as the new database that we're replacing the old one with implements this interface from the applications perspective it can still call close and select user so it doesn't care whether or not the database solution changed so you could think about it like this like maybe in the application code will close the database or something or we'll select user in the code and let's imagine originally we're using a nosql DB mongodb and in the application code we're selecting user using this method right but then one day some business need arises and we need to swap out this database for I don't know MySQL so this guy needs to move and we replace it with this MySQL now from the application perspective MySQL implements this interface like it has the plug so from the perspective of the application it can still call close and select user and it doesn't need to change anything right because this database solution implements this interface so it provides close and select user so its plug matches the shape of the port of the interface so this interface is providing a layer of abstraction so all of this stuff here is abstract that is the application doesn't need to know about any of the details happening in here just needs to know that it has these two methods that are defined in its interface and from there the application is completely decoupled from the implementation details of the database solution now that is an interface so with this understanding of an interface now understanding things like polymorphism or different design patterns like abstract Factory pattern Etc becomes a lot easier because now you have a basic understanding of wooden interfaces so now let's get into a quick coding example all right so with our new new found understanding of interfaces let's go ahead and quickly write up the code for the application from the example so I already have a module set up here so in this directory I'm just going to create a new file and it's just going to be the main.go file and this is where we're going to put the application code and the package should be main here and we're basically just going to do exactly what I mentioned in the video so we're going to create the interface and that's going to be the connection point for the database and the application essentially so just bear with me for a second it'll become clear soon so we'll just do type DB contract and that's going to be the interface and in order for the database implementation to be able to connect to our application it's going to need to implement this interface or it's going to need to conform to the shape of our interface so we'll do close as one of the methods that needs to be implemented by the database implementation and we'll do insert user and it's going to need to take any username which is a string as the parameter and it's going to need to return an error and lastly we'll just do select single user and that's going to be username as parameter as well but this one's going to need to return the user and an error so this is the contract that the database implementation needs to follow or conform to to be able to plug into our application so we'll just create a struct here for the application and we're going to need to em Ed the DB into the struct and the type for DB is going to be the DB contract so this is how we're saying that the DB implementation needs to implement the interface and again it's going to become clear soon so just give me a second so we're going to go ahead and write the code for essentially creating a new application so we'll do Funk new application is going to need to take in a DB so we need to pass in the DB to be able to create the new application and then we'll say it's going to be type DB contract which says that the DB that we pass in here is going to need to implement this interface or conform to the contract and what's going to be returned is a pointer to the application struct and we're just going to return essentially an instance of the application with the DB embedded so when we create a new instance of this application for lack of better term we're going to pass in the DB which needs to conform to the contract and then we're going to turned the structor the new instance with the DB that we pass in here embedded into the instance and then from our main function that's where we're going to actually create the application and stuff like that so basically we're going to first need to create the DB so we'll do DB a equals and we'll start with mySQL db. new now as you can see I already have this created here and that's because I already created the two database implementations here so so we have a MySQL DB implementation and a postgress DB implementation the code's basically identical for both just one's MySQL and once postgress and in the implementation code if you go down you'll see that we can create a new DB using new here this new function and that's going to return a new MySQL DB and all this code here is doing is just connecting to the mySQL database and then it's going to return the MySQL DB so what's important here actually is this struct here this struct is actually the object essentially that's going to be returned from this new the call to this new function so in main here when we call mysqld db. new it's going to return an instance essentially of the mysqld DB so in main here when we create this new one we're going to get the DB back which in this case is going to be the MySQL DB so if we go back here how do we know that this DB conforms to our contract from here like how do we know that it's implementing these well if you go back into the DB code here and we go down to under where we're creating this new DB we have the function here for closing the DB and this syntax here in simple terms is tying this function to this MySQL struct and this MySQL struct is this struct up here so if we create essentially an instance of this MySQL struct here it's going to be associated with this close function here and the same thing for this insert insert user function since we have this here in this syntax it's going to associate this insert user function with this MySQL struct and same thing for this select single user this syntax here is associating this select single user function with this MySQL struct so if you're coming from an object-oriented language then essentially you can think of this as a class this MySQL struct as a class and you can think of this new function here as the Constructor for the class so to create a new instance of this class then you would use this new function here and you can think of these as the methods the class methods and the way that we're saying that this close method quote unquote method is a method of the myql classes by using this syntax here now a lot of Hardcore go developers might have issue with me explaining it this way using oop as an example and they might even have an issue with me using this here for the receiver variable and to be honest I don't care like go make your own video but anyways this is how I'm explaining it because this is the best way to explain it so that it's easily understandable if you want to think about it from an oop perspective and if you don't want to think about it in terms of O you can just understand that if we use this syntax here we're associating this function with this MySQL struct by using this syntax so that means that this MySQL struct is actually implementing the interface here because we have the clothes the insert user and the select single user functions defined the implementation for those functions defined here and this is how we're associating those functions with this MySQL struct so if you see DB contract here when we create a new application we expect type DB contract so that means that we can pass in that MySQL struct because it implements those functions described or explained in this uh contract here so what do I mean by that so what I mean is here we can create this MySQL new and the resulting DB that we get we can pass it in when we create new application because if you go here new application takes in type DB contract or it takes in this uh interface here and the myql struct implements that interface so all of that to say that if you remember from the explanation I had the diagram with the application and the application needs to connect to a database well here this is where we're instantiating that application so app equals new application and we're connecting the database here when we pass in this DB as the parameter you can look at this as plugging in the database to the application at this point we're plugging the database to the application because this is where the interface or the connection point is so we have a DB here and we have an application here and we're connecting them both here when we pass in the DB here and the reason we're able to pass in the DB here is because it conforms to our interface so for example if we were to just do something equals and then just put string here and then we try to pass something in here then of course it's not going to work because as you can see down here it says that we cannot use something because it doesn't implement the DB contract and it's goes on into further detail saying that some methods are missing like the close method Etc so anyways back to this we can put the DB back there so if we go back to the code for new here we need to pass in the DB user DB password DB host port and DB name so let's just go back to main here and we'll just go ahead and create all of those variables as well and of course these are just dummy values for now and then we'll just go ahead and pass those in and of course we need to check if the error not nil and in that case we'll do log. fatal because we need the DB connection for the application and let me just minimize this and since we created that close function we can do db. close or since we implemented the close function we can actually defer db. close and if we go to the definition of that you see it's this close function that we created or implemented inside of the MySQL DB implementation so back in main we're going to defer the closure of the DB and we can delete that and now we can actually write some application code that's going to make use of the DB so the same way that we Associated functions with the MySQL DB struct we're going to do the same thing for this application struct so we'll do Funk and then we'll do this and it's going to be the application and we'll create a function called run and you can just imagine this is the application code or whatever so we'll do username equals user one and we'll do air equals so now we caness access the database we can do this. DB and that's because remember when we create this application when we create an instance of this application down here we're passing in the DB which is then embedded into the struct here and we can now access this property using this this variable here the receiver variable here so we can do this. DB do insert user and we can just pass in the username and if error not equal nil we'll just log. print error and we can also call the other uh function so we'll do eror equals this. DV do select single user and we pass in the username as well here and we'll just copy the error handling here and now we have the user at this point if we were able to get the user here and we can just print that user so this is just like some sample code for running the application so if we go back down to the bottom of the main here so now we have the app so we can just do app.run now I'm going to make this a bit smaller so I can explain so like I mentioned before this is the connection Point like this is where we're connecting the DB implementation with our application via our interface so if you want to imagine it the same way from a physical perspective when I talked about the audio interface and like the plug plugging into this this new application you can think of this part as like the port where you're plug pluging in the plug now like explained before one of the benefits of this is that we could swap out the database because as long as it conforms to the interface then we can use whatever database right so here I created this MySQL DB and this postgress DB so we could actually just change this here so if you go up here you see that I'm importing the MySQL DB right now but I can also import the postgress one and back down here instead of doing MySQL do new I can just do postgress db. new and as you can see everything still works exactly the same like if you go to this run here we don't need to change anything in run even though we swapped out the databases because all run cares about is being able to call insert user and select user doesn't care about the underlying implementation so as long as whatever database you write the code for here the implementation that you write the the code for here conforms to our interface here then we could swap it out or connect it to this application and we won't need to make any changes to the application at all and that's basically it like that's how interfaces work can go and I think that if you can understand interfaces you will become one step closer to being a much more advanced goang developer but anyways that's going to be it for this explanation if this video was helpful for you please don't forget to leave a like and if you're not already subscribed go ahead and subscribe I make a lot of videos explaining intermediate and advanced concepts and go and I also make videos explaining algorithms and data structures and other computer science topics so if that's something that you're interested in go ahead and subscribe and if you're interested in enrolling in my course on microservices using the go programming language the link is in the description below and with that being said I'll see you in the next one
Info
Channel: Kantan Coding
Views: 8,311
Rating: undefined out of 5
Keywords: golang, Go Programming
Id: IbXSEGB8LRs
Channel Id: undefined
Length: 21min 54sec (1314 seconds)
Published: Thu Apr 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.