Golang Interface Basics You MUST Know!!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] yo what's up ninja said welcome back here to the girl like dojo this channel is all about the girl programming language if that's something you fancy make sure to give that subscribe button a nice little tab in today's video we are talking about interfaces interfaces are extremely powerful and can do quite a bunch in ego so make sure to pay attention in today's tutorial video here in front of us we have a summer pre-written code as you can see we have a type ninja star which consists of a owner field which is of a type string also this ninja star type has a one method called attacked it doesn't really do anything besides putting out hey we are throwing the ninja star as a attack method here in our main function we have a ninja star slice that is being assigned to this variable here the ninja star slice that consists of a two ninja stars both of which belong to the owner wallace now in order for us to use both of these endangered stars that we would need to have a for loop and they call the attack method for both of these ninja stars now let's go ahead and run this program as you can see it is putting out throwing ninja stars twice and it makes sense because this ninja's star slice consists of two ninja stars now of course ninja stars aren't the only type of weapons that we have in the ninja world let's say that we have another ninja weapon type called ninja sword here and this ninjas with this ninja sword we can also call this method attack and then the attacking method doesn't do anything besides swinging the ninja sword now in order for us to use it there's a newly created ninja weapon type ninja sword we will need to create another ninja sword slice that consists of a two different ninja swords that both of which belong to the owner wallace here and again we would have to create a separate for each loop and inside of the 4-h loop we need to make sure each of these is injured source are calling this attack method in order for us to attack with these a ninja sword now as you can probably already guess and this isn't a viable strategy because as we are adding more and more weapon types that also means that we would have to create more and more of these weapon slides and then creating different a separate voyage loop in order for us to attack with these newly created weapon types and this is where interfaces come into play as you can see we can create go ahead and create this ninja weapon interface and inside of this interface we have this attacker function that we need to implement for all of the newly created ninja weapon types and then we can just recall those attack method and pass in whatever ninja weapons that we want then we can go ahead and call the attack method with these uh passed in and ninja weapons and the way to implement these interfaces in go is more similar to how you would do it in python than doing it in java in java for example in order for us to implement interfaces in java we would need to go to the define type or class and type in implement and then the interface itself in order for us to implement interfaces in java however in go in order to implement interfaces it is done a little bit differently it actually golang actually applies a concept called typing duct typing is a programming concept that states if it walks unlike it dug any cracks like a duck then it must be a duck what that implies in go is that as long as it is a newly created types such as a ninja star or ninja soil as long as they implement all of the functions for the interface and ninja weapon here aka attack which it is implementing it because we have a tag function for ninja saw we have a tag function for the ninja star as well as long as all of the functions in this set interface are implemented in these newly created types and then these newly created types are also of a type this ninja weapon this interface that said we can now group both of these ninja stars and the ninja swords into one single ninja weapon interface type as you can see we are creating a new ninja weapon slicer and be able to add both a ninja star and a ninja store into this ninja weapon slice and assign it to one single variable rather than having two separate slices like we did before now we can go ahead and use a for each loop in order for us to iterate through the weapons a bit of of a type of ninja star or a ninja sword and be able to call this attack method and as you can remember the attack method is going to take in a weapon of a type in ninja weapon as a parameter and then it called the attacking method for this ninja weapon being passed in if we now go ahead and run this program again as you can see it is printing throwing ninja stars or twice because we have a two ninja stars in this ninja star slice and we are swinging ninja source twice because we have a ninja sword two ninja swords in this ninja sword slice and we are calling throwing ninja stars at once and swinging ninja swords once as well because we have it there's a ninja weapon slice that consists of one ninja star and one ninja sword so this is how you would use interfaces in go i know interfaces can be a slightly more complicated topic that confuses a lot of beginner programmers so make sure to leave all of your questions in the comment section down below i try my best to answer all of your questions with that make sure to subscribe to this channel and i'll talk to you next time you
Info
Channel: Golang Dojo
Views: 12,873
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go tutorial, go programming language, golang tutorial for beginners, golang crash course, golang for beginners, Golang Interfaces - ULTIMATE Golang Basics Tutorial, golang interfaces, interfaces in go, golang interfaces explained, golang interfaces tutorial, interfaces in go tutorial, understanding interfaces in golang, learn golang interfaces, golang interface tutorial, go interfaces
Id: 2T6qePP1BWE
Channel Id: undefined
Length: 6min 52sec (412 seconds)
Published: Sat Jun 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.