From the course: Learning Go

Reference values with pointers - Go Tutorial

From the course: Learning Go

Reference values with pointers

- [Instructor] Like C and similar languages, Go supports the use of pointers: Variables that store the memory address of another variable. You can declare a pointer with a particular type, but you don't have to point it at an initial variable. I'm working in the main function of my main.go file in the practice directory. And in this branch, I'm starting off with a brand new application. Above the output command, I'll declare a new variable using the VAR keyword. I'll name it P for pointer and I'll set its type to * int. The asterisk means this is a pointer, not a value. And if I don't assign anything, that means that that variable will be nil; it doesn't contain anything. Now I'll change my output command. I'll start with a label of value of P and then after a comma, I'll output P. If I just name the pointer variable like this and then run the application, I'll get an output that says that's a nil variable. But if I add the asterisk before the variable name, meaning, "Hey, this is a pointer "and I'm pointing at some other variable," then I'll crash the application because P is pointing at an invalid memory address. So that's what happens with a pointer that isn't pointing at anything. Now, let's see what happens with a pointer that's pointing at a valid variable. I'll create a new variable at the top of the code that I'll name anInt, and I'll set its value with colon equals to a literal value of 42, an integer. Now I'll change this code. I'll once again start with var p and then I'll say that equals &anInt. The ampersand character means I'm pointing at the memory address of the variable, not at its value. And when I run that code, I see accurately that the value of P is 42, and that's because it's pointing at the value variable. Here's another example. I'll start with a floating point value. I'll create value1. I'll set it to a value of 42.13. Then I'll create a new variable called pointer1 And this time I'll use the colon equals syntax and I'll point it at &value1. Now I'm not explicitly declaring the type of pointer1, but because I use the ampersand, I'm pointing at the memory address of value1, not the value. To output this value using the pointer, I'll use a label of Value 1 and then *pointer1 and the asterisk says, "This is a pointer, "and I'm pointing at this other variable." And I get that value. So now let's see what happens if you try to change a value through the pointer and how it affects the original variable. I'll say *pointer1 = *pointer1 and I'll divide that say by 31; could be any number. Then I'll copy and paste this line of code. And I'm going to do it twice. The first time I'll say, this is the value if I address the pointer, and the second time I will address the value directly with value1. I'll run the code and I'll show that when I change the value through the pointer, I'm also changing the value that it's pointing at. If you're coming to Go from Java or C#, this is very similar to those languages' reference variables. If you're coming to Go from, say, Java, this is very similar to that language's reference variables. If you have an original variable and then a reference variable that points at that reference variable, you can change that variable either by changing the original variable or by changing the variable that's pointing at it. But unlike in Java, the pointer doesn't have to point at any particular value initially and you can change it at runtime to point from one value to another. If you're used to pointers in C, C# and other similar languages, you'll find pointers in Go are very similar and just as valuable.

Contents