Transcript from the "Pointers" Lesson
>> So let's talk about pointers. Pointers in Go, are a variable that holds onto the memory location of that variable instead of a copy of its value. And so kind of what we just saw, demonstrated by trying to modify that describe group function is that we're just modifying a surface level copy of that variable's value.
[00:00:20] So it might be changed temporarily, but it's actually not going into that memory location and modifying that variable in a permanent way. To do that, we need to add a little bit more syntax, and we need to use something called a pointer type. So notice on line 6 and 7, we're creating placeholders for two different variables.
[00:00:37] The first is a variable called name which is set to a string. And the second is a variable called name pointer, which is also set to a string, but it has a little asterisk before the word string. And so this is telling Go that instead of just giving me a place to store a string as a variable, I need to provide a little more information.
[00:00:55] I need to actually store a memory location into this name pointer variable, not just the name of a string. Cool, so I'm gonna take this exact code and move over to our pointers.go file. And we'll see what prints out in the console when we have both pointer types and normal string types.
[00:01:26] Cool, so the top here, here's a copy of the code we were just looking at in the slide. I'm going to change my target directory here to run our Section seven pointers, code pointers.go file. So what we're printing right now is these placeholder values. We haven't assigned any value to these actual variables.
[00:01:45] We just have placeholders for name and a name pointer which has the asterisk. And we're asking our console to print out both of these. So see that our name variable is giving us back that empty string, which based on how Go defines default values that makes sense. And then for the pointer value, we're seeing this nil.
[00:02:04] And the nil indicates that this pointer value is expecting an actual address in memory, not just an empty string, this is a special kind of variable that we need to provide more information for. So let's dig a little deeper here. [COUGH] If we were to actually assign some variables here, our first one is Beyonce, and our name pointer, let's go back to Lizzo.
[00:02:35] You'll notice I'm already getting an error. But if I want to then go ahead and print these out again, you'll see that we're gonna get a type error. We cannot use Lizzo, which is a type string as the same thing as the type pointer string. So fundamentally these are two very different types, either a memory address location or a string value that's a copy of what lives in that memory address location.
[00:03:00] So in order to indicate that instead of just a string, we actually want it to be a memory address, we use the ampersand. And so on the right-hand side here, I'm going to say, instead of just a string, I'm going to use ampersand. And I'm gonna steal the variable name here since we have it conveniently defined right above it.
[00:03:25] So I'm saying okay, the name is gonna be set to the string Beyonce, but the name pointer, go get the address, read through this pointer to find the address and give me back what that looks like. And so now you'll see the two variables have been modified a little bit, that first one is still typical name string, but that second one is actually giving me an address now.
[00:03:49] So it's not the value, it's the address of where that value lives. So now that we know that we have the address of that particular variable, if we actually wanna read through and find the value that lives in the address. We're gonna kinda deference the variable we just defined using that asterisk one more time.
[00:04:32] Cool, so let's kinda talk through all the stuff that has happened one more time. At the top of our main function, we're establishing two different placeholders for variables, one of which is going to be your typical string. And one is going to be a pointer to where a string variable lives.
[00:04:50] So we just want to access the value of the variable, we can assign it to a string. But this ampersand here actually indicates we wanna read through and find the address. So I like to think of the ampersand as a forward address, get the address of that variable.
[00:05:04] And then lastly, let's say when we print this variable, we see that big convoluted address and we actually want that value back. We need to use the star once again and say okay, so I know you're an address, go read through that address and give me back the value.
[00:05:17] So it's kinda this relationship between do we have the address and what do we want? Or do we need the address and how do we get it? So ampersand for get the address star for I have the address, give me the value. One of the main differences to keep in mind is that the star either lives next to the type name or the variable name.
[00:05:43] If the star lives next to the type name, that indicates that it is a pointer type. So the only thing that this variable is going to accept as it's value is an address. If the star is next to the variable name, that means that it is a pointer variable value.
[00:05:59] So it started as an address, and now you want that value back. And then to read through the variable to see the address, ampersand for A, go find the address. And that ampersand lives next to the pointer variable, name.