This Will Make Everyone Understand Golang Interfaces

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up ladies and gentlemen in this video we're going to take a little deep dive into interfaces in golang I already covered interfaces and going a couple of times but still people are confused about them and that has nothing to do again with your skill level it has to do with coming from other languages uh where things behave where things are getting done we're stiff or getting done differently you know what I mean and then you come to golang and then you have the concept of interfaces and yeah it's it's kind of hard to grasp your mindset around that concept well no problem we're gonna make another video you ask and I delivered that's what it is but before we continue if you like my videos guys please consider subscribing to my channel give me a thumbs up I need some questions in the comments jump into my Discord community and for the people that really want to level up I have the full-time code F course again at 50 discount it's actually more as 50 discount but that's a marketing trick is what it is but still is a 50 discount right so if you want to buy it it's not your chances right now it's today it's it's the once in a lifetime opportunity guys I swear to God check it out coming to this good Community ask people what you think about that so you can actually have some real time reviews about it okay enough marketing so basically I have this uh beautiful stuff going stuff laid out already and we're going to talk a little bit about football players why I'm taking this example is because everybody knows that right it's not an NFL player it's a football player soccer in some countries football player what uh does every Football let's say you are a manager right you're you're a manager of a football team and you need to assemble 11 players right if you need to assemble 11 players and you're gonna take it from a high level perspective right not go into the details but just from a high level perspective and you have a list of thousand football players that basically or basically subscribe to your to your team or willing to join your team what do you expect from them right not knowing the details right you don't know if it's Ronaldo it could be messy it could be Holland who knows could also be I don't know your neighbors you know what I mean this this fat lazy you know what I mean it could be him right but we assume that the people that basically subscribe to the list for joining our team they can all kick a ball you know what I mean they can kick a ball that's very important right there's a contract between me and my players that if you are going to be in my team you should kick a ball you know what I mean you should at least know how to kick a ball you could say yeah but some people are basically have more accuracy they have better technique they they they they they hit the ball with the left foot with the right foot some people hit it with the boat Foods whatever it is uh the inside the outside of the food there's a lot of things that can happen is completely true but that is basically details player details uh specific you know what I mean and that's not what we care about right we're just gonna assemble our team first so we have hit a football play how if you think about that how can we translate that to Gola and code while I have this this minimalistic thing set up here we have a type as a football player it's a structure right uh it's a stamina and power um and like I said they have one function right one function only which we are interested in right now and that's going to be cake ball right and we have some crazy algorithm going on here we have the stamina plus the power is going to be the shot but I mean it's just I don't know I sucked that out of my dump and then we're gonna print out that I'm kicking the ball and we're gonna print out the shot which is calculated from Dash Fields right their statistics their stats so uh if we enter our main function here which is where all the magic happens right we have a team we're gonna make 11 football players you know what I mean football 11 players one goalkeeper of course but of course he also needs to kick a ball let's be honest so then we're gonna Loop over that team we're gonna um We're Gonna Fill our array or slice to be politically correct and we're gonna do some random number c from one to ten and then we're gonna range over our team over a football team and we're gonna all let them kick the ball right that's what it is and of course I run this already just to see that it's working I'm going to say golden main.go heat eyes and girls and we're gonna see that they all have uh these things and the last one is basically the best one and then we have this this poor little guy here with Forge or history even he is basically sitting on the bank for the whole season that's for sure right so that's that so we have a little program here fine and you could see everything is working perfectly fine so uh if we think about this right pause the video or something and think about this code right if you if you're gonna look look at the score is it seems okay you know what I mean there's nothing wrong with this code right that is like I said before and I'm gonna say it again there is no such thing as bad code or good code it is always something it's it's an ancient old scam where people are basically discussing all about and it doesn't really matter uh in my opinion um the thing that really matters is the end result but there are some problems there could be some problems here with this code right and it all starts with the football players right they call can kick a ball but we also discuss like okay there are some players that have a better technique right they are also players that are much faster they are some players with with um Factor you know what I mean for example if you have Cristiano Ronaldo or you have Messi or Harlan or something they have this room you know what I mean they also have a celebrity status and all that stuff and if you need to calculate how they're gonna kick a ball it can be very complicated heat right because you're going to have different types of football players you're going to have a slow football player you're going to have a left foot football player you're gonna have maybe a two-footed football players you're gonna have a football player which you're gonna have a football player with butt and a football player with Foo and if you want to calculate all that stuff in your kick ball heat for your football player you're gonna end up with a with a field and you're gonna end up with a lot of fields in your strictly you know what I mean look at all these field seed and then you're gonna make uh if and if stuff if um whatever right and you're gonna have a lot of if statements in your kickball and if you need to test that it's going to be very very complicated because a lot of stuff can happen right maybe some hard-coded values and all that and all that Shenanigans you're gonna have a bad time right with these problems most of the time those do not pop up from the beginning of your of your program they come as the time goes by right and then you're basically a little bit stuck your programming you're gonna hit a wall you need to reflect it and you're gonna have a bad time luckily of course if you come from other languages like a python or something that really object object-oriented approach you're going to have a base football player and then you're gonna have uh all these decent inheritance stuff and you're gonna have a good time but in gooling we don't have that concept we have the concept of interfaces right so let's see how we're going to refactor this thing into an interface so the first thing we know is that every football player the only thing we're concerned about in this simplified guys simplified higher level example the only thing we um are concerned about is the kick ball function right so an interface is basically a contract between me and something else most of the time a structure well actually always a structure it's a contact between me and some kind of arbitrary structure where I don't care about what it's doing as the internals as the implementation as the goat which sits in the function we signed a contract Fort or functions the only thing we care is that if I call this function it's going to do something that's the only thing right if we make each a type football uh players interface right and we only care about the Kickball function right kickball actually two else probably all right and of course this football play is going to be in big problem because we have a football player uh at face lights basically to play and hit so it's going to be working pretty fine right we're gonna have a player interface and it's going to have a kickball we don't get if it's going to kick the ball hard if it's going to kick the ball with a left foot with their right foot with both foot at the same time we don't care the only thing we care is that if we call kickball it's gonna it's gonna kick a ball but how we don't get about that right we just want to call this function without being by the compiler without getting a runtime problem you know what I mean so how do we do that so we have a pledge we have a kickball function now we need to basically change some stuff up here right so we have a football place which is basically a slice of the direct implementation of the football player strict we don't want that we want to have a slice of players right 11 slice of players so um yeah the thing is that skateboard yeah it's fine so what we're gonna do here let me um change this function up here right so why do I do this because if I hover over this problem it's going to say cannot use football play it the value of a type football player which is the structure type football player as a player right so football player and player we cannot assign these two together why because football player does not implement the player interface ah and why because it's missing the method kickball so if we delete this exit it's going to be worked perfectly fine because we have a contract right we have a contract between our team they are players and the contract is I don't care I don't care what type of structure you're going to insert into my slice into my team as long as the structure you Place into it has a kickball function I don't care maybe it's a function that does nothing we don't get maybe it's a function that just prints a string we don't care I just need to kick a ball hitch kickball right so this opens up the opportunity for some clean code why because we have this football player here we can actually copy this thing right now boom copy display football player we're gonna call this for example or not we're going to call it cs7 it's cs7 right seven which is Cristiano Ronaldo and the Seven comes from his back number which is seven you know what I mean and we're gonna say stamina power we're going to give him some soon we're gonna we're gonna get him soon so that's what you're going to give him it's an integer it's a special integer only Ronaldo has you know what I mean um and we're going to say heat um we're going to implement that right so we're going to copy this King ball function heat we're gonna paste that in eat like that I'm gonna delete some spaces you could also save but it's fine I'm gonna say F it's gonna be CR7 right and CR7 is going to kick the ball but we're gonna say F stamina plus power and we're going to um times two you know what I mean that's what it is right so right now what you could do actually is we're going to make 11 football players so we're gonna land T minus one to be honest right just like that and now I'm gonna say uh Team uh LAN I'm gonna do something crazy guys land team equals two that's gonna be this is gonna wreck my video I swear to God it's gonna be CR7 you know what I mean something like that and then we're gonna say all right so we've got a loop actually ten times to be honest right and then uh the last one we're actually going to see it's gonna be CR7 and we're gonna say uh stamina I'm gonna give him 10 of course and then we're gonna give him his uh some ball with right it's gonna be ten and I'm gonna give him some soup but it's also it's gonna be eight right so actually ten because it's from him right it says wood something like that I know it's gonna work let me see um to be honest what I'm gonna say is I'm kicking the ball you're gonna say heat uh CR7 CR7 is getting the ball it's freaking ball yeah uh okay cool yeah yeah it's all fine guys it's all good you always see it coming but I also so basically uh look what's happening guys everybody's kicking the ball with these rookie numbers and then we have CR7 is kicking the ball with the Sue swung fact that attached to it and he's this this shot is is not going to be stopped right it's not going to be stopped you know what I mean unless you're courtois which is also a Belgian plate by the way that's why Belgium is superior that's not true but you get my point right so you can see right and instead of doing uh in this football player in the beginning instead of having some suheed and then we need to check if the player is here is if the player is Ronaldo now we need to say that we need to print that uh CR7 is kicking the ball otherwise we need to say that I'm kicking the ball and uh and what what happens if you have if we have messy into the team not then then we need to see if if it's Ronaldo do this if it's messy do that and and anybody else do this right and then you have something else Roberto Carlos from back in the days I don't know if you remember him but he was orbee's Aryan Robin you know what I mean he looks a little bit like me or maybe I look a little bit like him um Robin you can see we can have a list of these players and it's always going to be if else's right and that is going to end up in very very unreadable quotes but right now it's very easy basically what we could do is actually just say um you can just copy this whole shebang right boom just like that and I'm gonna make a new one here you're gonna call this messy you know what I mean well let's say that Messi also has a sue Factor um doesn't really matter just for the sake of this demonstration heat so we're going to say messy right um and then we could say for example um this this is nasty code what I'm doing here I'm gonna lie um this is what it is right let's add message that is so bad but it is what it is guys uh Messi and then I'm gonna say Susan because Sue is from Ronaldo you know what I mean um something like that right must escape the ball we have some problem here because I prob never do what I do close your eyes right just like that so you see cosm is kicking the ball and then Messi is also kicking the ball but the problem is that Messi has a little less because messy Sue factor is not that great it's not that good as the man himself right uh so that's basically it I mean if you do not understand this simplified video guys then I am very bad teacher then then I'm a very bad teacher but I think you should um you could also do something with circles and squares like like the go uh playground and all that stuff but like I said you need sometimes you need to think a little bit differently uh in order to to lay to make these logical connections inside of your brain right and the only way we can do that is to take some real world scenarios where you can feel comfortable with where you can feel familiar with and Implement that reflected into quotes start with something that doesn't really uh fit is the best way and then build this up and I hope this explains interfaces and you could say but yes how can we know the name here right so for example let's see let's say let's do a lasting before we close this video let's say we delete this right and we delete this stuff and we want to see uh maybe this is going to return the shot factors right it's going to be an integer so we're gonna actually return this right return because we're going to change this up kickball is going to return an integer it's going to return the shot factors power whatever you want to call it so what we need to do is we need to return this stuff instead of printing it out I'm going to say that it's going to return an integer just like that and and then we're going to have this football player and heat uh delete that return okay you could see that right now they have the same logic heat but if they have different different kinds of Statistics you're going to see that's going to differ right so take take that in in mind and now we're gonna have uh instead of basically he didn't kick a ball uh we're also going to say team um you could actually do an fmt S print fee you're going to say percentage s is kicking the ball just like that and then maybe we could do a percentage D I'm going to say uh Team dot y dot name right which is not gonna happen and I'm gonna say team y uh dot cake ball right it's gonna return the shot so it's going to have a problem here which is name right you could see main undefined the play it has no field or matted name that's completely true because we only have cake ball right and we cannot access these fields here from uh you could actually say for example name it it's not going to change right it's not going to change change what we need to do is we need to add this function here name it's going to return the string right it's going to return the string and now this function is going to work perfectly fine but you're going to have a problem because all these guys hit they do not implement the player interface why because we added a function name but they don't have that function name so we have a contract and somebody is basically not following the contract so we need to change that well how do we do that very simple uh we're gonna make a function here Frank f going on F CR 7 heat name do this this string return uh the CR7 right CR7 boom just like that and then we're gonna copy this we're gonna paste a t for Messi right messy messy and then for this football player uh let's do Funk f football players name this string return I don't know a known right we don't know what it is random a random guy random boom just like that right so now everybody's happy right so we're gonna do this thing we could actually do this F print F to be honest printf my bad right so let's run this again go run Main dot go right so now yeah of course um can we do a new line yeah we can uh clear the screen right so now uh random is kicking the ball because we don't know with this random play we don't care right um and then CR7 and Messi is kicking the ball right so now we basically um removed again some logic from our implementation stuff and we actually uh place that heat because we added an extra function to the interface right let me know what you think about this um I think when I was coming into this concept I would have a good clue on what's on what's going on right now I hope this simplified uh example help is helping you to have a better understanding right and go like interfaces and especially why do we need them right right why do we need this interfaces because otherwise our football player structure that we had in the beginning is going to grow so hard that it's never going to fit any doors you know what I mean again if you like this video consider subscribing and leave some questions in the comments jump into my Discord community and check out the full-time code if you're interested and I'm looking forward to see you in the next video or live stream peace
Info
Channel: Anthony GG
Views: 45,294
Rating: undefined out of 5
Keywords: go programming language, golang tutorial, golang, golang interface, go interfaces explained, go tutorial, golang tutorial for beginners, golang interface tutorial, golang for beginners, interface, go programming tutorial, go interface tutorial, golang tutorials, interfaces, golang interfaces explained, programming, go programming, learn golang, go tutorial for beginners, golang programming
Id: rH0bpx7I2Dk
Channel Id: undefined
Length: 21min 2sec (1262 seconds)
Published: Mon Sep 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.