Golang Tutorial #3 - Go Pointers Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys and girls of course welcome to the third video of this go tutorial series if you are completely new to pointers or you are completely confused about pointers then this is the video for you a lot of people find pointers to be one of the more challenging topics and programming but i don't think it has to be i think a lot of that is due to the way that it's taught so my goal is that by the end of this video you know exactly what pointers are and you know exactly how to use them because i don't think it has to be nearly as challenging as people find them to be so let's get started let's first define what a pointer actually is a pointer holds the memory address of another variable so that's exactly what it is it's the memory address and when it comes to pointers and go there are two symbols that you have to consider when thinking about pointers they both start with a they are the ampersand and the asterisk let's talk about the ampersand first the ampersand basically has one meaning when we're talking about pointers and you can imagine that it translates literally into the address of and it's the address of the variable that it proceeds for example if i was to say a is equal to 25 and we said b equal to the memory address of a the value of b is just that is the memory the address where the variable a is stored and its type is a pointer to an end and we write that as asterisk followed by the type the asterisk actually has two meanings you will either see it next to a variable or you will see it next to a type when it's next to the variable we're saying that we want to get the value that this variable or this pointer is pointing to and that's called dereferencing a pointer you're getting the value that it's pointing to for example above we said that b is equal to the memory address of a if we were to set n equal to asterisk b we are d referencing b we're saying get the value of a and set that equal to n so n is equal to 25 the second case as i said is you'll see it next to a type and when we see it that way we're saying we're basically declaring a variable that is a pointer to that type so just like we did above we said asterisk int weird b is basically a pointer to an integer type so that that is the type of b asterisk int and i'll give another example uh i could say var pointed a string my name and basically we're saying that my name holds the memory address of a string variable hopefully that makes sense if it doesn't i'm going to give you definitely give you more examples now i'll give you not only example in code but i'm a big fan of the visual illustrations in fact i think they're highly underrated when it comes to programming uh and i i love to like dabble a little bit in vr and ar so i kind of put this little program together just to give an illustration what we see here is a program that allows us to create a variable we can put in the type we can put in the name we can put in the value and then it'll get stored in a memory address and you can see that these boxes on the left here actually have addresses so for example let's say i want to create a variable called my name of type string and i'll set that equal to z to gerald and create that and then it gets stored in a memory address pretty simple so let's go ahead and reset that and first i'll type out what i'm going to illustrate so let's say i want to create a variable a and set it equal to 25 and remember this is implicit declaration so the compiler will determine that this is an integer automatically now go ahead and do this in the visual program okay so we've got that variable there now i'll say b is equal to the memory address of a so let's create that and you can see here that the value of b is in fact the memory address of a and when you're writing a program there's nothing there's nothing special about these memory addresses you can easily print these out and i'll do that right here and there we can see the memory address of a next i'll say i'll set c equal to the memory address of b now this one is a little bit tricky when it comes to the type that's the question for you is what type is c and i'll give you a hint it's not a pointer to an integer because b itself is not an integer b b is actually a pointer to an integer so what is c and i'll show you guys in this visual program you may have already seen it on the list here and so we can define c as a pointer to a pointer to an integer i know that's kind of kind of confusing but that's what it hopefully this visual illustration uh does provide some clarity for you and i also could have written it like this if i comment out the other line you can see that the program still runs fine and of course i had to de-reference c there in order to actually print out the value that c is pointing to so that right there is really pointers i mean there's certainly more to it but if you can understand everything that i just said right there i promise you like understanding pointers will not be an issue for you now there is more to understanding where and why you need to use pointers so let's go ahead and talk about that one use case for using variables would be to alter the value that's passed into a function when we call a function that takes an argument and by the way i haven't talked about functions yet but you guys have certainly seen me use them you've seen me use the main function you've seen me use probably the print line function more than anything else and what what's what i've done is basically pass in values or arguments to those functions we'll talk more about that later but for now just know that you can pass in you know these values to a function and they you know execute some type of code but when you pass a one of these basic values which i talked about last week primitives and to a function you're actually not passing in the variable you're only passing in the value it's actually a copy of the variable and so the actual variable remains unaffected and i'll illustrate that uh with an example here so let's say i create a function called zero that takes an integer and then it sets that value equal to zero and then i'll actually create a variable in my main function to illustrate that so we'll set x equal to five we'll pass that into this function zero and then we'll print out the value of x and what you can see here is that x is still equal to five and that's because like i said we're only passing in a copy of the value once it's passed into the function you're not you're not working with the actual variable anymore and this is important for example let's say that you had a program that was some type of price calculator and you know you start out with this initial price which is stored in a variable and you want to pass this around your program that price that applies discounts to that price maybe it it takes off a certain amount based off of aids maybe it takes off a certain amount if they're a member or something like that you may be thinking that this value is actually being i'm sorry this variable is being manipulated as it's being you know passed into your functions but it's actually not and you need to know that because you'll get back that value it'll be the exact same as when you start it and one way we can get around that is by using pointers so for example if i say at the top here instead of passing in an integer i want to pass in a pointer to an integer and then i want to de-reference this value or this pointer x so x at this point is a pointer that's what this function that it says a pointer to an integer and if i want to change the value i need to de-reference it i want to say okay i want to set the value that this points to to zero okay and then down here instead of passing in uh x to zero we can pass in the memory address of x because we're accepting a pointer into this function we're accepting a memory address in this function and i could have i probably should have used a different variable other than x within the main function it didn't have to be x it could have been anything but i'll go ahead and print that and then we can see that it actually has changed the value has changed to zero so that's the first case for why you would want to use a pointer as i said to actually change the variable that's being passed into another use case for why you would use a pointer would be to reveal the value actually has been set or if it's just the default value for example let's say that you're working on the back end for some movie streaming service and you're building a service that collects data and part of that data is whether or not the user is 17 because you need to determine whether or not they have access to rated r content let's say that the people designing the front end application let's say they goofed up it's not actually sending you any value so the user you know they hit this check box in the app that says they're 17 uh and then it's supposed to send it back but you don't get anything at all and let's say i'll start off let me go ahead and define this variable here so variable is 17 that's a boolean so this is what we would set and if you watch the video last week i talked about default value types at the end of the video and the default value type for a boolean is false and the default value type for a number of other structs and types that actually have not discussed yet as well as pointers the default value is neil and neil means nothing it has no value at all so it's false for boolean it's kneel for pointers if you don't set it to anything that's what the value would be so like i said the front end doesn't send anything the user clicked it but you didn't receive that value that means that it's going to be set to false no matter what so let me go ahead and kind of illustrate how a pointer can rectify that so let's define a pointer is 17 set equal to a pointer to a boolean and let's let's go ahead and just print out both of these values and just by printing the value you can already tell that the pointer hasn't been set yet because it's kneel that's the only way you could get it could be nil if this nothing was ever set to it but you wouldn't know that looking at the value of just a boolean now let's go ahead and take that a step further and let's create a variable input which is a boolean and we'll set it equal to false and we'll set is 17 pointer to the memory address of input that's what it is it's a pointer to a boolean and then we'll put we'll print out uh both seventeen pointer and print out the dereference of is 17 pointer if we can see we can see that we get the memory address and we get the value of false when it's d reference and we don't get any nil values because it's actually been set so like i said if we were just using the standard boolean uh it would either be true or false but you actually don't know if the value has been set and i mean that's pretty much pointers in a nutshell it doesn't get that much more difficult than that we'll talk about pointers a little bit more when we talk about uh receiver types and custom types in a later video but i encourage you guys to read up on it um please do if you guys have any questions or feel like i didn't explain explain something well put that in the comments please do subscribe like the video that does it for this tutorial thanks for watching i'll see you guys in the next video bye
Info
Channel: Esoteric Tech
Views: 1,810
Rating: undefined out of 5
Keywords: Go Pointers, golang pointers explained, go pointers explained, golang pointers and references, Golang Pointers, pointers in go, Golang Tutorial, Golang memory, Go tutorial, go programming, go programming tutorial, learn go programming, pointers explained, go programming tutorial for beginners, go tutorial for beginners, golang course, learn golang, programming go, golang basics, golang tutorial playlist
Id: 1_KR3udN6Q4
Channel Id: undefined
Length: 13min 29sec (809 seconds)
Published: Sat Jan 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.