GoLang Interfaces | Beginners Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back to nerd academy i'm your host and in this video we're going to talk about go interfaces so let's go so at a real high level a go interface is merely nothing more than a type that kind of defines or says what type of methods that interface should be interfaces allow gulling to be more object oriented kind of in a way and we all know going really isn't object oriented but this is about the closest we're probably going to get so in other languages say like java and c sharp they uh interfaces are kind of explicitly uh implemented i mean they have java has a keyword implement and go is more kind of implicit so this is kind of uh along the lines of structural typing structural typing is where type compatibility are defined by the structure and definitions in which that kind of determines if two types are equal or equivalent so when go interfaces if two types implement the same methods defined by the interface they will be considered equivalent and they can be both that type that the interface defines now the interface says what type of methods or what methods that type should have while the types are left up to the implementation of those methods so each type that implements those methods the the methods can be completely different from each other they just have to have the same return type and parameters so interfaces really only specify the behavior the method names and the return types etc and the types implement those methods so the best way to understand this is kind of just by practicing and doing so let's go to a quick example over in our editor of choice uh we're gonna let's let's make an example of let's say a bookstore so let's make a couple of types make a book which has a title a string and it's going to cost something so we'll say price call that float 32 and let's say we also want to sell drinks who knows like a book and a coffee right so drink let's say it has a name and it's also going to have a price so let's say we just wanted to print out information about each of these types so we'll have to create two methods we'll use the value receiver i'll print info all right that's going to print out uh the price the drink name and the price it's like make another function let's uh print out the book info all right now we got two methods that are gonna do the exact same thing so let's go ahead and make a book then we want a cup of coffee all right now we can print both of those gunslinger dot print info and cop go ahead and run it go run main.go there we go they both printed out now you must be thinking there's probably an easier way to do this well you'd be right this is where interfaces come in much like this uh the structs it's keyword type name of your interface so we'll just call this printer interface what we need to do in our curly braces is define the methods that the interface should have so we'll just call we'll just do print info so now both of these types have the method print info same return type which is none and no parameters but both drink and book both implement the method print infos now they are both either type book and the print the printer or drink and the type printer let me show you what i mean so let's make an array of printer items so let's call this info and we can pass in both pass in both the book and the drink because they both implement the method print info so they are now of type printer now if we want to we can do the same thing by using the info slice and it's going to do the pretty much the exact same thing if we run that again it just prints it out twice so let's say for instance we have another type that i want to sell some kind of toy in the store also this also has a name let's say this time it's gonna call print toy so now this print toy method is going to print the name of the toy and the price let's go ahead and make a toy let's say rubik's cube price five dollars okay the same thing we did before let's just print the toy you see that works so the the toy prints out rubik's cube and price five dollars but what we cannot do pass a rubik's cube to our interface slice it doesn't like that the printer it is not a printer because it does not missing as you can see there here it is missing the method method print info and to fix that all we have to do is make the toy implement the method print info and now it is of type printer as you can see the error went away now we can use that printer item and print the info like we did with the other ones oops there we go so that wasn't so bad was it but let's just take this one little step further and kind of use some deduction and some logic here since we can create an interface and kind of extend any type where in our example our book was a book and our interface printer same with the drink so if we follow this logic a bit we can have empty interfaces that will satisfy any type including built-in types and user-defined types think of it this way if we have an empty interface it's it's defining that no method needs to be implemented so any type can be of this empty interface now you have probably already used methods that use this empty interface let's go take a look at the format fmt uh all those print methods so if you're using like visual studio code or code open source you just control and click on the method and since we have golang installed locally we can access all these methods or you can just go to go to the web and go to their documentation because it's fully documented there too we take a look at this printf method has a string and takes this weird thing called a dot dot dot interface which means it takes between zero and as many empty interfaces as you want to pass to it which means it takes a zero as to many types as you would like to pass to it so if you take a look at all these print methods fprintf s printf we got f prints we got just print we got s print f println println s println you get the idea all those print methods accept all the same thing this this empty interface type so let's take a look at a simple example of how how that works so let's make a very simple kind of method here we'll call this empty call this i for interface we've got to use curly brackets there so we're going to do something called switch we're going to switch on the i we also have to do this weird uh notation where you go i dot and then type this kind of just gets the underlying type of what that interface is so we can have multiple cases cases are kind of what it evaluates to it's kind of like a fancy if-else statement if you don't know anything about switch switch statements so we use the keyword case and if this is a string we're gonna do something with a string so we'll just use our favorite fmt print f i'm a string in the case of the string we're going to print out i'm a string and then print the value of the string and if this is a oop ins do the same thing print if i'm an int we can also check in case this is of type book from kate's book we're going to say on a book i'm going to print out then the name of the books or the title to that we have to do more kind of fancy stuff we're going to go i dot's book that saying it's a book and then we're going to get the title and print that up so go ahead and test that out empty we're going to say james empty we'll pass an int 24 23 and empty we'll pass the gunslinger to it and run that again and there you see a time string i'm an ant and i'm a book well that was kind of a very basic overview of interfaces and going as always the code will be in the link in the description below and if you want to see more about going watch this video here see in the next [Music] [Music]
Info
Channel: NerdCademy
Views: 927
Rating: undefined out of 5
Keywords: go programming tutorial, go tutorial, go tutorial for beginners, golang, golang empty interface, golang for beginners, golang interface, golang interface tutorial, golang interfaces explained, golang interfaces tutorial, golang programming, golang tutorial, golang tutorial 2021, golang tutorial for beginners, golang tutorials, learn golang, programming tutorial, software development, software development tutorial
Id: KB3ysH8cupY
Channel Id: undefined
Length: 7min 55sec (475 seconds)
Published: Thu Sep 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.