Practical Explanation of Golang INTERFACES

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so goang interfaces are one of the most important things to learn if you want to become good at it initially I had mixed feelings about them because I already had half a decade of experience working with other object-oriented languages where interfaces are pretty similar among them but in go to act differently so it took me a bit to get used to them however if you check my videos you'll notice that I end up using interfaces in pretty much every video and that is how important they are for developing good software in go so in this video I'm going to give you a practical explanation and then a Hands-On real world example of how interfaces work and how they can make you a better software engineer by being able to write better more modular and testable software firstly an interface is a type so you can Define them like so with the interface keywords and all it does is that it holds signatures for a set of methods for example here we have let's say a sense a message for a user where you pass the user ID and here we have the N64 and and we can return for example an error so this is the signature of the methods that would implement this interface here and here a medium basically is a way of sending a message to the user it could be a SMS it could be an email it could be a pigon it could be anything and so the fun part comes when consuming this interface now because they're abstract ideas this means that any method that implements this exact signature it sets to satisfy the medium interface and here for example we have the SMS implementation we have here a struct that implements this exact signature and then this medium SMS struct is considered an implementation of the medium and the same here for the email and it's important to note that there is no implements keyword in go like there would be in other languages so whether or not a type satisfies the interface it's determined automatically now let's expand this simple example to something that you probably are going to be doing which is building an API also if you're looking to learn more about how to build production grade API in go I have a free video course on the description below so I'll leave the link there if you want to check it out so here on the main.go we have a simple HTTP server just using the standard Library I also have a video covering that as well but basically here we have just a simple NBAs connection a router and we have a service that we create and then the register route that we are going to see now this handle here is very simple how it does is that it passes the payloads and then we validate it as well so the last basing part is that we need to register the user and for that we could just create here a function and call it today for example register user where we pass in the user payloads register user payload and we could return an error and here we would need to go to the database and do the insert in the table so for example here we could pass the database instance uh let's say like so and then here we would do something like this so so basically just inserting into the user table and then here we can consume it let sketch the error register user we pass in the payloads the database and this would be it so we would need the database has a dependency so let's add it here and we also need it on the Constructor and here on the main we can just pass in our database that we are creating here and we can just pass it in the service like so now here in our test if we C this service end point test it would fail because we're not connecting to the database or worse if we are mutating the database we might not even know it so for example to fix this we could pass here the uh connect the SQL here and pass in the database so this would work but this is just a simple unit test we don't want to connect to the database so we need to mock the database completely which is not it's not doable cannot just mock the whole database uh you might be able to do it but it's not a good practice and so we need to mock these methods with side effects that go to that external storage and this is where interfaces really shine now in most other languages you would most likely need to install any external library for example to do mocks to do steps however here in go we can just use the power of interfaces to solve this very easily Not only would you already be using interfaces in your services because they make injecting dependencies very easily and natural but also make your code way more modular and we're going to see how that works here in a bits so instead of passing the whole database implementation here let's create an interface and pass that interface here for example going to call this store and it's going to be the store um the user store and let's create a new file called store. go now let's create here A type called store which is going to be a struct now the struct is going to receive the database implementation from before then I have just created here simple Constructor to help us initiate the store but the most important bit is going to be here the user store interface that is going to do all the magic now this interface is going to receive the register uh user that we have created before so let's just go back here and get the signature which is like this now we don't need this database part here because we already have it here has a dependency so this is all we need and we need to pass in as the error as well then let's just quickly implement the method for the store so the store is going to be valid implementation of the user store implementation which register user and it's going to receive the register payload and it's going to return an error now the implementation is going to be the same but here we access the database from here now after this is done let's go back here and delete this method we don't need it we can also uh delete this database here and we need actually the store this is going to be the user store and here the the same finally here on the main let's delete the database from here and let's create a new store so it's going to be the user store and I just do new store and all we need to do is pass here the database and then we pass in the store interface for the service so the service now is completely independent from the storage so we can pass here anything as long as they implements the user story interface now you can see how powerful this is this is because here in our tests we can instead of pass a database you can pass a mock store just like so now this mock store is just a Str that is going to implement that method so here let's just create a simple struct without anything attached to it but then here we say that the mo store is going to have the register user with the same exact signature so it's going to also return an error and here we can do whatever we want we could just return a real user it could return anything um let me just fix this so here we could even return a user could return anything I'm going to return nil and here we have a valid interface here now if we removed for example the error it would be invalid so if we go here go and compile already tell us that it is an invalid signature and to fix it all we need to do is have the error here and now if we run our test we are sure that we are not mutating or messing up any database that we might might have connected in our invar and finally here on the service we need to fix this by accessing the store and then the register user we don't need this here because we're not dependent on the implementation of the database and this is how you can use interfaces in your goang service handlers so if want to go to the next level and level up your go skills here are some resources that I would recommend you reading so for example you have the go.d section on interfaces it's pretty lengthy and they even talk about some stuff that I didn't menion and I would also like to recommend the Jordan Ori blog post here about interfaces I really liked this one and he also speaks about the type any interface here and with that thank you so much for watching and if you're looking to level up your goang skills I also have a fre Discord community on the description below so if that interests you go check that out and see you on the next one thank you
Info
Channel: Tiago
Views: 4,487
Rating: undefined out of 5
Keywords: golang interfaces, go interfaces, interfaces in go
Id: 4OVJ-ir9hL8
Channel Id: undefined
Length: 8min 26sec (506 seconds)
Published: Sat Mar 30 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.