Golang Tutorial #21 - Struct Methods

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody and welcome back to under golang tutorial so in this video we're gonna be talking about methods and for those of you that don't know what methods are essentially they are some function that we perform on an object now in this case we can actually make our own methods for these type of objects now an example of a method would be something so this is a student example that I've done here it has a name it has a list of grades so a slice of grades that are int and it has some age an example of a method that we could use on students would be something like get grades right and what that would do is return to us all of the students grades another example could be something like is older than 18 and that could maybe return to us true or false depending on if the age was older than 18 another one might be get average grade right if we have a bunch of grades inside of this slice so we have you know like 50 70 80 maybe we would want to be able to get the average grades what I'm trying to do is just make you think about some operations that we might want to perform on a student and that's specific to a student right only a student object will have the method get average grade because it'll be specific to the types of fields that are defined inside of this object now I hope that's kind of making sense you've probably seen methods before and going I can't remember most of the ones we've used so far but the idea is if we have some variable let's say var X in equals 5 right um this isn't a good example but we can write the variable we can do a dot operator and then just like we accessed when we had a student you know the dot name what we'll do this time is we'll say dot get grades right if that's the name of the method we'll put some brackets because methods are typically they're just kind of functions that act on a specific object and then maybe inside of here we pass some value or this returns to us some value we store it in an array or whatever it may be or a list or an INT or whatever but this is the idea of a method you write dot whatever the name of the method is and then some braces may be you pass a value maybe you don't but this acts just like a function except on a specific object so let me show you what I mean let's make a student first of all let's say s : equals let's go student let's give it a name let's call it Tim let's give it a I believe I have to do this a slice of grade so we'll say that Tim's grades are 70 90 80 85 okay and then how old 19 all right so we've just created a student now what I'm going to show you how to do is how we can actually make a method that will return to us let's start really simple and it just can't return to us the age of the student so what we write is this we write func because we're just gonna make a function that acts on a student object now this is where it gets a bit weird we put brackets and then we put some variable name that we want to represent our students in this case I'm gonna put s we put the type that we want which in this case is gonna be student and then here we put the name of the method so this is saying that this is gonna act on a student object that's what this first set of braces looks like and then afterwards I'll say something like get each okay then I'll put my brackets and now I just write a function like I normally would so here it's gonna return an int and then I'm gonna put my braces like that so what this is saying is this is a method that acts on a student object it's called get age it takes no arguments or parameters and it returns an int so now what I can do is I can say something like return s dot age and what this is saying is that when I call and I go down here and I go s1 dot get age this s is equal to whatever student I called this method on so since I called get age on s1 and we have these brackets set up before here s inside of this method will be equal to this student so if I want to access the age of that student I would just do dot age and then I can return that and that gives me the age now I know a lot of you are probably like why am i doing that why couldn't I just do s1 dot H you totally can I mean I get it this is not a great example but I'm just trying to show you in a simple case what you can do you have this s which is the student and then now you can reference any of the fields from that student do things with them return them whatever right and you call this on a student now let's say I went in here and for some reason actually let's change this to set age so we want to change the a you'd rather than get the age well now we gotta actually change a few different things first of all this is gonna be set age we're not gonna return anything we're actually gonna take probably some age as our value so we'll say age int and now inside of here I want to say s age equals age so I'm changing the students age to be equal to whatever age they passed in so maybe I say s one set age and I put seven well then we would expect that this students age would change to seven now I want you to think though very carefully will this work and you probably won't know the answer to this but we talked about pointers in the previous video and this is where they become really important if I actually want to do this operation when I'm setting or changing anything about the student object so anything about this here I actually want to change it so it permanently changes then what I need to do is make this a pointer so I need to make that a pointer and now what I call s 1 dot set age I'm hoping this will actually work we'll see if I save this if we get an error or not I think we're good yeah so if I change this to a pointer now what I'm saying is when I call s 1 get age I actually want to get the pointer of s 1 so that inside of here I can modify the age value and it will change down here I hope that makes sense but let's FM t dot print line let's print s 1 and then let's print s 1 again so let's have a look here if we go and go run tutorial go and let's see what our output is okay so we get Tim and then obviously it has the age 19 and then afterwards it has the age 7 so this did actually change in here because we had the pointer now if I change this and I get rid of the asterisks let's have a look at what we get now 19 19 so although here yes we did change the age since we didn't pass the pointer we weren't able to change it down here it just changed kind of that local copy that got passed into this method so the rule of thumb kind of here is that if you have a method that's modifying this object itself you actually want to make sure that you have the pointer here and for most methods you're always going to want to use the pointer inside of this so that you have your variable that's actually pointing to the original object so you can modify you can change things if you just care about getting values or maybe like manipulating values are calculating something which we're gonna do in a second then you don't need the pointer you can just take the value but common practices just take the pointer anyways because it's not really gonna matter if you take the pointer versus not take it when you're returning a value so I hope that makes sense but what I'm gonna do now is to find another method and I'm gonna call this method actually get average grade so I'm gonna say s student and I'm gonna say get average grade like that and we're gonna return a actually this is probably have to be a float 32 that will be the average grade so inside of here now what do I want to do well I want to calculate the average grade of the student now this doesn't need to be a pointer because I'm not gonna be changing anything about the student I'm just gonna return a float 32 value it tells me the information about the average grade of whatever student we have so first of all let me get rid of some of this which means I'm actually gonna have to comment this out so we'll do that like that and inside of here I'm going to start writing how I calculate the average grade so I'm gonna make a variable called sum I'm gonna say son colon equals zero oops there is easier ways to do this but I just want to walk you guys through an example here and we'll say for underscore comma B so that's the value colon equals range of in this case it's gonna be s dot grades you can see it's even popping up for us there then so this is accessing all the grades right which is a slice of int we're gonna say sum plus equals V so we're gonna add whatever the grade value is to the sum and then finally we're gonna return the sum over in this case it's gonna have to be a float 32 value of the Len of s dot grades so this should give us the average we sum up all the values and then we divide by some float 32 just to make sure that this is float 32 and in fact this is gonna have to be float 32 as well the Len of s dot grades which should give us the the answer right whatever that average grade is so let's have a look at this I'm actually gonna say it let's say average colon equals s1 dot get a bridge why is it not popping up here get average grades okay and then we're going to go fmt dot print LM of average okay so I don't think I made any mistakes there I know I went kind of fast oh well let's see oh this is plus I colon equals range that might have been the problem there let's have a look get average grade what's the problem here s 1 to get average grades undefined oh that's s needs to be get average grade okay so let's see now if this works let's run the program and we get the value eighty one point two five so you can see this works now the reason this is a useful thing to do is because what happens when I have another student right because you could say well oh I can just sum up the grades down here that's fine well when you have another student that you create right and you want to get the average grades of them so I say like s2 equals Jo maybe we add another grade here so we said like 90 90 95 let's change the age like 21 now if I want to get the average grades of this student well all I have to do is just say average 2 colon equals s to get average grade since I've made this method now I can use it on any single instance of a student so any s 1 s 2 doesn't matter any student I have I can use this method so it becomes really useful so now I can print average and average too so let's have a look at this and let's see what we get okay so we get eighty one point two five and eighty five point seven one four two nine so that is kind of the basics of methods now of course like you've seen we can take arguments inside of methods they work the exact same way that a function works except now we have a reference to whatever object it is that we're calling this method on so when I do s1 dot get average grade s becomes s1 and then down here s becomes s2 so here of course we're not getting the pointer so we just have the values of both of these but if we wanted the pointer it's as easy as changing that to an asterisk and now we can actually modify any of the fields inside of that object right so that is kind of the power of structs and that's right when we have things we want to represent that are more complicated than just an int or a bool like they're a combination of fields we make some custom object in this case I've called it a student a student has a name which is a string grades which are slice of int and then an age and now I made this method so that any student I make I can get its average grade now let's do one more and let's make a function that gets the maximum grade so let's say func S student we don't need the pointer here but I'm gonna put it here and we're gonna say gets max oops I could type this properly get max grade and then in this case we're just going to return an INT because we know it's just gonna be a slice of n so we're looping through so we can do that so now I'm gonna say cur underscore Max or cur max we'll keep our pattern here equals colon equals zero we're gonna save for yeah we'll go underscore v : equals range of s dot grades and we'll just say if cur max is less than V so this is the great then what we'll do is we'll say cur max equals V there you go so essentially if the current maximum value is less than whatever this grade is then curve maximum becomes equal to V and then we can return cur max so this is just storing the current maximum value as soon as we see value greater than it will change it and this will get us the max value from the array so we return cur Max and now let's actually just change this instead of average we can say get max grade just copy that method and throw it here get max grade and then of course we'll just say max and max2 and then this will need to change to be max oops and max - okay so let's have a look at this alright so go run tutorial dot go and we get to 90 and 95 so you can see that works properly here the high screen is 95 here the highest grade is 90 and of course you can go on and make as many methods as you want and use them in combination with each other really it doesn't matter it's all zoom out a bit so you guys can kind of read everything that we've done here but we made the student struct we made some methods for the student I showed you how we can modify fields on the student as well we can make another method called add grade and all that does is append to this slice another grade that would be something that's maybe useful to do and yeah that is kind of the introduction to methods so I hope you learned something if you did make sure you leave a like subscribe and I will see you in another golang tutorial
Info
Channel: Tech With Tim
Views: 20,461
Rating: undefined out of 5
Keywords: tech with tim, golang methods, methods golang, golang object oriented programming, golang, go programming language, go programming, go programming tutorial, methods in golang, golang struct methods
Id: 5b8MMXgBnp0
Channel Id: undefined
Length: 13min 41sec (821 seconds)
Published: Thu Jun 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.