Golang: The Last Interface Explanation You'll Ever Need

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so interfaces are one of the most common things you will use and understanding them is a real must but mastering everything including interface composition or the combination of errors and interfaces or even understanding the io. reader and io. writer interfaces can be really challenging especially for beginners that's why I've decided to make this video your last video about goang interfaces oh and by the way if you're new here my name is FL professional software engineer and on this channel we do everything related to the software engineering world so let's quickly start by looking at some basics of when and why to use interfaces in golang so you can think of interfaces like contracts for instance of methods for specific types like structs and these interfaces provide some kind of highlevel organization Without Really implementing the business logic of these methods so by just the finding the Declarations or the definitions of these methods that the drugs should Implement and this sort of flexibility and kind of grouping of these methods gives us developers a really nice flexibility and highlevel organization and a struct for instance like I said before can Implement these interfaces by just embedding or implementing the logic of these methods that were defined in the interface and as long as as the struct has these specific methods in the interface the struct also directly implements this interface so let's quickly take a look at what interfaces are in goang code all right I've got here an empty goang test project and what we are going to do now is to simulate a shape interface and in the end we do want to have a rectangle in circle for instance that implement the area function and obviously a rectangle has a different area equation compared to the circle equation so let's quickly Define an interface here so for that I'm just going to make use of the type keyword here and generally the type keyword in go Lang just means that now in new type definition starts and here again like in instruct for instance the visibility matters so for instance if we capitalize our name of the interface then obviously this interface will be public however if we just start small so for instance we say shape this interface will be private for now I will just make it public and then say interface and with that we actually now got a new interface now what we're going to do is just to define the contract of our shape that for instance the rectangle or Circle should Implement and like I said before we are going to make use of the area calculation here and with that in mind we're just going to define a function in our interface and here it is really important that we are only going to define the function definition without any implementation so we're going to say area that's basically it and obviously we can Define the return type here so for instance we say float 64 and now we got our first contract method in our shape interface so let's quickly Define two structs the first one is our rectangle angle and this just includes the width and the height which are both float 64s and then we obviously got a circle and this circle just receives for now the radius all right now we got our two structs which obviously are some shape and we have to implement this area function now it's important to know that for now golang does not know or the goang compiler does not know that rectangle and Circle actually accept the contract of our shape interface so how can we achieve this contract implementation here so there is no specific keyword like I said in the beginning we are just going to implement this shape area function in our two structs and then we accept the contract of our shape interface so for instance we're going to declare a struct method here by just using R and then then we say rectangle and then we say area and here is the return type float 64 and with that implementation we now kind of accept or implement the shape interface by just defining this function so let's implement the logic here by just saying return r. width * r. height now this obviously is the basic area calculation of a rectangle so with that in mind what for instance can happen if we Implement a new method here or we refactor the shape interface to have a new method for instance let's say ABC then obviously the rectangle does not anymore accept the contract of our shape interface because obviously the ABC method is missing in our rectangle struct but if we remove this ABC method the area method gets implemented by our rectangle and therefore a rectangle is now a shape all right let's do the same thing with our Circle so we say funk then we Define a simple struct method here and then we just calculate the area of a circle now let's quickly make use of these two structs here and obviously of our brand new interface the shape interface so let's just create a rectangle and circle let's just say rect is a rectangle and here we say withth five and height four and then we Define a circle which is of type Circle struct and here we say radius 2 and now we obviously want to call these area functions right but let's just say that we have a new function which should call these area functions in our structs so let's say we declare a function called calculate shape or let's call it calculate area and this function is pretty generic because it can accept as a function argument a shape interface so let's say s and then shape and then in the end we are just going to return a float 64 and in the end we are just going to call S do area so what does this function even mean here first this calculate area function has as an argument a shape so in the end there should be like some sort of type or struct as the argument which accepts and implements the shape interface or contract and then in the end we just call this function definition which will be for instance the circle area function so if you're just going to say for instance fmt do print line and in here we're going to say rectangle area and then we say calculate area and now we say rect this function calculate error gets called with our rect struct so the rectangle struct and in the end the rectangle struct area function is going to get called instead of just a plain interface function here and because obviously the rectangle implements the shape interface this calculate ERA function is going to work with our rectangle structure let's do the same thing with our Circle and this will now basically work right so if we are just going to run our project here we get the correct rectangle area and obviously all also the correct circle area so we now kind of have a generic shape interface and two shape structs which are rectangle and a circle and then we've defined a pretty generic function which is called calculate area and this function takes in an argument which will be an implementation of the shape interface which in this case could be the rectangle or Circle and then we are just going to call the area function which is implemented by our rectangle and circle all right with that in mind we can do a lot more with interface types because an interface type can hold pretty much any value so for instance we can say mystery box and then we can say interface now this looks pretty weird but in the end interface with this curly bracers is just a empty type which can hold any type and then we say 10 and this now here just holds the interface value 10 now you might think why is this even necessary so let's just create a function here now we are going to call it describe value and this describe value function just takes an in t which is an interface just an empty interface and this empty interface basically means that it can accept any type obviously in goling they are generics so this is a pretty broad implementation and I obviously don't really recommend this so use generics as much as possible because they obviously have their use case but now for instance we can make a fancy print F statement and in here we say for instance type and then value and in here we will just use T and T so in the end it will just print the type of the past in value and the value itself so let's just test this by just calling it describe value and then we say mystery box and now if we run this code we will get the type in and value 10 and this is pretty neat now obviously we can do a lot more with that so for instance we can do type assertions here so let's just say retrieved in and okay now this is pretty Advanced but with that syntax here we can try to cast our mystery box which is of type interface or any type to an end and if this succeeds we can do something like for instance print line line and then we say retrieved int and here the type is directly an integer which is also pretty nice so we don't have to do anything else here but if it fails so if the mystery box is not an integer then for instance we can just say value is not an integer now if we're going to run this obviously it says retrieved in and this is pretty good but if we just say that the mystery box is a string and then we're going to print this again we going to get first off the typ string and then value is not an integer and with that in mind we cannot only use the interface here as some sort of contract but we can also use this interface as somehow a generic type so it can also be any type if we use this specific syntax here all right these were the basics of interfaces in goang let's quickly go into a more advanced use case so I'm just going to quickly clean up this code here and let's just say we don't need to Circle anymore because now we are going to make use of interface composition or more likely embedding interfaces into each other so for that we are just going to define a new interface let's just call it measurable and this measurable interface just takes in a definition of the parameter function here and let's just create a third interface and we're going to call this geometry now here it is pretty nasty because we can embed interfaces into each other so for instance we kind of want to inherit or compose the measurable and shape interface into our geometry interface so for instance we want to say that the geometry interface should have the definitions of our shape and measurable interface and this way we create an interface to combine multiple interfaces and this is pretty neat cuz I'm going to show you why this is even powerful here so with that we can just say shape and measurable and then we basically embedded interfaces into each other so the geometry interface now has the function of our shape interface and measurable interface all right let's let's quickly remove the circle here and then we're going to implement the parameter function for our rectangle struct all right now we got our perameter function for our rectangle struct so now the rectangle struct here accepts the contracts of our measurable interface because we do have the parameter function and the shape interface because obviously we do have the area function all right and now we want to actually refector the calculate error method to be describ shape because we just want to print for instance the parameter and area function of a geometry interface so so no matter what we always want to call the parameter function and area function so we're just going to say G and then geometry and in the end we are going to say fmt do print line and in here we're going to say area and then G do area and the same with the parameter and this now obviously works because geometry embeds the shape interface and measurable interface so we can call these two function area in Perimeter without any additional logic and now what we're going to say is just say describe shape and then we call rect right and this now works because rectangle fulfills the contracts of our measurable and shape interface because in the end we do have an area implementation and a parameter implementation for our rectangle struct and if we're going to run this we're just going to see area and parameter all right one more thing I actually wanted to show you is using error interfaces in our goaling code and with that in mind we can just declare a new struct let's just say calculation error and this struct is going to have a message which is just a string because obviously we want to describe the error as much as possible here and then we are going to implement or use the arror function the aror struct function so we just going to say c for instance instance calculation error and then we're going to say error and the return type is a string and in here we're just going to say return c. message and this is a pretty common Peta you will see in huge goaling code bases because I'm going to show you that a native goang error actually only implements or is some sort of interface right and this interface has a definition of this specific error function here so let's just create for instance a new function and say perform calculation and then we're going to say the value which is a float 64 and the return type is a tuple where we do have a float 64 as the calculated value and maybe a possible error and this is also a pretty common pattern you will see in golang here right let's just say if the value is less than zero obviously we do not want to perform the calculation for whatever reason we're just going to say zero as the default value for our return float 64 and then we're going to say calculation error with the message invalid input now if not we are just going to say for instance the square root of our value and the error will be n okay let's quickly go into dep here what we've actually done so the error type is in in the end just an interface which uses which uses the error function definition so if you're going to go into the source code of goaling we are going to see this definition here right so we have an interface which is the error and this interface has the error definition with a return type of type string and here we have our contract right and because we now Implement in our calculation error the error function with the exact same definition of this error interface this calculation error is now a valid error in our goal end code and that's why this function here does not give us any specific error that was returned by the compiler right this works because we implement the error function of the error interface all right now these were some important Advanced interface Concepts you actually have to understand to fully Master interfaces in goang and clearly there are more interfaces like IO reader and IO writer so feel free to experiment with these two interfaces on your own now right next to the golang interfaces are actually structs right and luckily I've made a video about them so feel free to check out this video here if you want to know everything about goaling structs anyway thank you so much for watching have a lovely day and bye-bye
Info
Channel: Flo Woelki
Views: 1,679
Rating: undefined out of 5
Keywords: golang interface, go interface, golang interface struct, go interface struct, golang interface pattern, go interface pattern, golang interface composition, go interface composition, golang error interface, golang interface tutorial, go interface tutorial
Id: SX1gT5A9H-U
Channel Id: undefined
Length: 17min 57sec (1077 seconds)
Published: Fri Jun 07 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.