Golang Tutorial #19 - Pointers & Derefrence Operator (& and *)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody and welcome back to the golang tutorial so in this video we're gonna be talking about pointers and the difference between references and values now this is again a very important concept I know I've said that about a lot of things in this series but this one really trust me when I mean this this will let you kind of get into more complex aspects of going and this is something that I'm gonna guarantee a lot of you probably won't understand on the first try so make sure you mess around with this on your own you follow with some of these examples I'm doing and that you know you try to listen to the whole video because there is a lot of stuff that I need to kind of build on and we need to start slow and then get to the end of it but pointers allow us to do just more complex things in golang and they kind of relate to what we talked about in the last video when I showed you those pointers from like the variable X stored you know address 3 and then address 3 pointed to whatever that variable was this is somewhat related to that so there's actually two operators in golang there's an ampersand and an asterisk and what the ampersand stands for is get the pointer and what the what does it Astrix stands for is dereference now this seems like a weird thing you know they seem kind of complicated I'm gonna show you in a few examples how they work so if I make actually let's just say like something like X colon equals 7 so there's two properties of what I've just done here we have what's known as a reference and we have what's known as a value so I've said X is equal to the value 7 so our value is obviously 7 that's whatever's on the right hand side of the equal sign what we're storing right what the data type is what information we have and then the reference is actually the location of where this data type is stored now in our mind that's X we're storing it inside of X in the computer's mind that might be just some other different location in memory right but regardless we have a reference which is pretty much X it's where the value is stored and then we have a value in that of course is just the value what the actual value is so to look at the reference of a variable so not the value to look at the reference we use the ampersand so if I do something like FM T dot print line ampersand X what this really says is tell me the reference of X so what that means by reference is where is the value 7 stored where is the location for that so let's have a look at what this actually prints for us and you can see that we get some random gibberish memory locations so this is kind of how a memory location looks you go 0 X and then a bunch of hexadecimals or I think this is a hexadecimal code I could be wrong on that but it's some you know some gibberish that stands for we're in our computer's memory the variable X actually sits so you see then when I did and X it didn't give me the value X it gave me the reference to X so that's something to understand and that reference we typically call a pointer now look what happens when I just print X I mean this is pretty straightforward but we just get the value right the value of x so we have the pointer and the reference and then we have the value and you access the point or the reference by putting an ampersand but before whatever it is that we're gonna be referencing or using now this is important because this allows us to to change things so let's just look at this example if I say Y colon equals ampersand of X what I'm saying is Y is equal to the pointer for X now that does not mean that Y is equal to 7 it means Y is equal to whatever that location is that we just looked at so like 0 X see whatever right and that might change as we run the program but regardless it's equal to that location to that pointer okay so that means now if I actually modify Y and I use what's known as the dereference operator which I'll show you in a second I can change the value X because just like I showed you in the previous video if we have the pointer right if we have the pointer to where this value is stored if I modify that value so if I change it inside of that block that this pointer is pointing to then any block that points to that block will be modified or any variable I keep saying weird names I'm sure is confusing but the idea is like we have x equals 7y is equal to the pointer of X which means we're referencing where 7 is stored so now if I use that reference and I change the value 7 it will modify so let me just fmt dot print line X&Y for you here and now what I'm going to do is show you how I can actually change the value of X through the pointer of X so if I do this and I actually do asterisks y equals 8 I can change Y to be equal to 8 now you're like what the heck is this Astrix Tim you're just throwing new stuff at me yes I am what this asterisk stands for is dereference so this says take this pointer value and dereference it which means access essentially the block so access where this is pointing to and then here I can modify where it's pointing to to be equal to 8 instead of 7 are not where it's pointing to but I can modify the block that it's pointing to to no longer be equal to 7 but be equal to 8 I'm gonna draw this out so this will make sense for you in a second but just look at what happens when I do this so let me just make sure I save here let's go go run tutorial go and we get let's have a look here ok so we get 7 some memory location than 8 some memory location so by doing this if I look at X right X changed from 7 to 8 yet here I didn't do anything with X I mean yes I got the pointer here but I didn't modify ax so I was actually able to change X through the pointer to X so this is the idea I'm storing the pointer in Y right and then if I actually want to change the value of where this pointer is pointing then what I need to do is I need to go asterisk Y so I need to dereference the pointer I know that seems like a weird thing but dereference it and then set that equal to some value so now look what is it X is equal to 8 and if I want to make y equal to no longer the pointer but to like the value I can say y equals asterisks Y actually that won't work because Y is it's a pointer type but regardless I've changed the value of X through this pointer Y so that I know seems really confusing but that is the basics of pointers that if I have a variable and this works for any variable by the way not just what is it not just regular variables like this works for mutable variables immutable datatypes doesn't matter like anything you can use these pointers I doesn't matter if it's mutable or immutable like the previous example but anyways we're able to modify X the value of X through the pointer to X so that's something that's interesting to note now let's look at an example of using a function then I'll get onto the whiteboard so if I say something like func change value and I go here and what I'm actually gonna say inside of here is I want a pointer to an int so I actually let's do string let's change it up let's say STR and then I'm gonna say amperes are not ampersand Asterix star now when you put an asterisk before a variable when you're putting this in a parameter what that stands for is I want the pointer for this variable I don't want the actual value I want you to pass me the pointer now the reason you want to do that to pass the pointer is because I can actually modify a variable here using its pointer but if you just pass in the value I'll show you what happens so let's say change value let's just go STR and I think I'm actually gonna have to do this I'm gonna say Asterix s TR equals changed okay now I'm gonna do another function I'm gonna say change value to and what this one's gonna do instead is this is just gonna take each string it's not gonna take the the pointer to it and it's gonna go here and it's gonna say string equals change okay so let's get out of this we can read all this here and zoom out a little bit okay and now inside of main what I'm gonna do is I'm gonna make a string so we're I'm gonna say to change colon equals hello like that and I'm gonna call both of these methods and show you what happens so I'm gonna just go do you like a change value to change but I'm gonna pass inside of here a pointer so because this first change value function it wants a pointer that's what this asterisk stands for I want a pointer to a string not just a string I want a pointer to a string that's the asterisks and then what I do here is I say okay well since this is a pointer if I want to change its value I need to dereference it so I go asterisk star equals change which means change the value at that so I'm doing that and then I'm gonna call that so I'm passing the pointer of to change which is the string to there and hopefully it'll change so after I call that I'm gonna FM t dot print line to change so we'll print it before and we'll print it after so we can have a look at what it looks like and now let me just run this and show you what actually happens and then we'll kind of discuss it here so go run tutorial dot go okay so we had hello and then we get changed so notice that even though I didn't change the variable to change anywhere here it actually got changed by this function the reason it got changed by this function is because I did not pass the value but I passed the pointer so I passed the pointer and since I had the pointer I was able to directly modify the original variable to change this is one of the very powerful things in golang that we can pass around pointers and use this dereference operator so now let me show you what happens when I change this to change value too let's see what happens now now I'm not gonna pass the pointer this time because this one doesn't take a pointer it just takes the string so let's have a look what is this saying here cannot use no that will be good here so let's run this okay so go around tutorial go and notice that our variable here stays hello even though I changed it here to change in change value - it doesn't change it stays - at the value hello the reason for that is because I didn't pass the pointer so when I'm using a variable that's immutable like a string right or that doesn't have those special properties like a slice and a map and I pass it through and I don't make it a pointer I can't directly change it inside of here what really happens when I do this is I say STR is equal to - change right which in this case is hello so STR becomes equal to hello when I don't pass the pointer which means I'm really just making a copy of the string that I passed here and if I make any change I'm only changing that copy of the string I'm not changing the original string right so if you want to do something like this typically you're gonna return a value right so maybe I'll say string string and then I would just return STR right and that would work I could return STR back and then we could reassign it down here but this won't change the variable that I passed because I'm not passing the pointer whereas up here I am passing the pointer so I'm actually able to modify it so now let's go to the whiteboard quickly and I'll draw this out and show you the difference between these two functions and what's happening on a lower level okay so I've just got on my whiteboard here I've just started drawing a little example here just for RAM just so we have something that's already kind of created I don't have to draw it out in front of you guys what I'm gonna do now is show you what happens when we actually use the code that I just wrote there so the code was pretty much like we made in a string or something right I mean this doesn't matter if it's precise but I said something like X colon equals high let's just use that as the example then I called the function change value so change value like that and I passed in to start that am percent now can I draw the ampersand that's the question I okay so you know what that's gonna be my ampersand I know this is not anywhere close to what it's supposed to be but just this is pointer okay this is the ampersand okay so we write the ampersand I know you guys are laughing right now in the comment section and what I'm gonna do now is put the variable X so when I call change value with ampersand X what I'm actually passing inside of here is a pointer now let me show you right so if I said X colon equals high then what we do in Bram is we'll say that like this box here is X okay so that's X X points to this memory address and this memory address stores the string high now forget what I showed you in the previous video with mutable immutable I kind of did a little shortcut there just to make it a bit easier to understand let's imagine that X and this address are kind of like the same thing okay they reference this box that stores high inside of them so what I do when I pass ampersand X is I actually pass not high but I pass this I pass this memory location so I passed this 0 X 3 7 4 so really this is your X 3 7 for some memory location ok so I pass the memory location I don't pass the value high now if I just put X there right so I don't put ampersand and I call so change value to like so let's say change value to then what I'm passing is X in here is not this but it's the value of x which is high right so I hope this is making sense so when I go to change value what I pass is the memory location of where this you know value is stored I don't pass the value itself which means if I have this I can modify the value so since I got given the address when I did that asterisks right and I said asterisks whatever we called the variable I don't even know it was passed and I called it string or something right someone who said string and I changed that equal to something what I was saying is change this memory block here change what's inside of it to be equal to change right so that actually changed the variable X up here because X is just pointing to this little block right so I think hopefully that's making sense but that's what I'm trying to illustrate here is that we have these memory address locations and we can pass those around instead of just the value because here if I just pass the value this doesn't tell me anything about the variable X it doesn't tell me where X is defined it doesn't tell me where high is even created right or where it's stored in the program we just passed that value which means when we get to that function we say STR equals x right so the string which was the parameter name I'll refresh you if we look at here we saw string right which is the parameter name so when we just passed the value let's go back here and have a look then we don't have any information about the variable X so we can't change it from inside the function whereas when we pass the pointer we do have information about the variable X and if we want to access the value we can do that and if we want to change the value we can do that as well so that is the core difference between a reference and a value and when you pass them to function this is what you want to think about do I care about the underlying memory location of where this object is do I care about possibly changing that or do I just want the value because there's a lot of times where we just want the value we don't actually care about modifying the variable that was passed in we just want to know what its value is equal to but there's a lot of times where we care about the variable that we're passing in right so in that case we want to use a pointer so hopefully that makes a little bit of sense and that that drawing kind of cleared things up okay so I'm just gonna summarize what we went over here again this is something that you're gonna have to kind of play around with and you're gonna learn as you go through but I just want to show you at least these operators and then the next videos this should start to make more sense with some more examples for channels and all the stuff where that we're gonna do so this asterisks when it goes to the left right yeah to the left of a datatype what that stands for is a pointer to that datatype so this is saying a pointer to a type string that's what that means when I do an asterisk string like that now when I use an asterisk before a variable name not datatype name what I'm saying is get the value of this variable so rather so this is saying take the pointer and figure out the value of this so if I say Astrix star I can change the value of it because I'm getting the value and then I'm modifying it with the equal sign that's what asterisks star does okay so I can change it like that now the ampersand if you put that before something that gets the address of that item the address are the pointer to this variable so it's not giving me hello it's giving me that 0x memory location that points to hello which means I can now modify whatever to change is equal to because I have that memory location so that is the core difference the asterisks is known as the dereference operator the ampersand is just get pointer that's what that stands for now we can do something crazy like this too so I'm gonna get rid of this function I'm gonna show you one last example that maybe will make this more clear so if I have another variable and I'm gonna define this explicitly and I say bar let's say pointer let's just call it that and then this is equal to an asterisk of string and is equal to ampersand to change now this is a valid line of code because what this is saying is I want to store a pointer declare did not use that's fine I can FM t dot print line it but this is a valid line of code because what I'm saying is that this pointer variable is of type pointer to string and then I assign this to the pointer to a string so now I have the pointer to the variable to change and I can modify it through this variable so if I print out asterisks pointer what this does and I know this is confusing cuz there's so many use cases for these things but if I print out Astrix pointer this will print out hello in fact I'll prove it to you if we go round tutorial go we'll get the value hello because this is saying find the value at the pointer whereas if I just print out the pointer right then we're gonna just print out whatever that memory address location is which is 0x c4e whatever it is right so we print that out so that is the core difference there now watch though if I try to do this and I'll show you that this will likely not work if I do the ampersand - pointer so let's go go run tutorial go and let's have a look at this okay so this actually this is interesting so this is a different location than okay so let me just say let's do this because I've gone to an example that I think is a little bit more complex for some of you guys but let me just print these out to show you okay so what I've printed here is I printed the pointer too to change and I've printed the pointer the pointer to the pointer so you can see that this first pointer here is the pointer that actually points to the variable to change which allows me to modify right and when I did the asterisks on pointer it gave me the variable hello now I actually got the pointer of the pointer when I did this so I said ampersand of pointer which is a pointer to a pointer now I know that seems confusing but this is pretty much telling me where the variable pointer is storing the pointer to to change right because this is storing a value but this variable is storing a pointer so this is really like when I do this I'm getting the pointer that points to to change where whereas here this is just a pointer that points to hello right so I hope that kind of makes sense maybe that confused you more than it needed to but try to mess around with these things and see if you can figure out how the asterisks works and how the pointer works again this is really hard to explain because we need like really good concrete examples and a long time to nail this in and I can't do you know two three hour long YouTube videos for a topic like this but please do make sure you understand pointers before moving too much further forward although if it's a little bit like questionable in the next videos that we'll be doing some more examples hopefully so maybe it should clear things up so with that being said I think I'm gonna leave the video here I hope this was at least somewhat helpful you guys and now you have some idea there between pointers and dereference operators which I've talked about here again this is a complex topic so don't feel bad if it doesn't make complete sense and I guess with that being said like the video if you enjoyed subscribe and I will see you in the next one
Info
Channel: Tech With Tim
Views: 25,606
Rating: undefined out of 5
Keywords: tech with tim, golang pointers, pointers go, go pointers and references, golang tutorial, golang programming, pointers golang tutorial, derefrence go, dereferencing
Id: a4HcEsJ1hIE
Channel Id: undefined
Length: 20min 34sec (1234 seconds)
Published: Tue Jun 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.