Join David Gassner for an in-depth discussion in this video Referencing values with pointers, part of Learning Go.
- Like C and similar languages, Go supports the use of pointers, variables that store the address of another value. You can declare a pointer with a particular type, but you don't have to point it at an initial value. The code looks like this. I'll declare a variable with the var keyword. Then, I'll name the variable p and I'll set its data type starting with the asterisk operator and then the type I want to assign. So, this pointer can point at any int variable, but right now it doesn't point at anything.
So, we say that it's null. In fact, if you try to output it right now, you'll get an error. So, I'll use my Println function and I'll say value of p, and then I'll output the value starting with the asterisk operator and then the name of the variable. When you want to get the value that the pointer is pointing at, you always start with the asterisk. I'll open a command prompt and I'll run that code. I get a runtime error.
The error message is "invalid memory addres or nil pointer dereference". So, I can handle that with a bit of conditional code. I'll wrap this statement with an if statement. My condition will be if p doesn't equal nil. Then, I'll move this code inside the code block. Then, I'll add an else statement and here I'll output the string p is nil. I'll save and run that code and I get p is nil.
Now, let's see what happens when I assign the pointer to a value. I'll create a new variable that I'll name v. I'll declare its type as int so it's compatible with the pointer and I'll set its value to 42. Then, I'll point the pointer at the value using the equals operator and then the ampersand operator before the value name. The ampersand means connect the pointer to this variable.
I'll make a copy of this conditional block and I'll evaluate the pointer again. Here's the result. This time I successfully get to the value through the pointer and I display it. So, you reference the value through the pointer using the asterisk operator. Let's see another example. I'll start with a floating point value. I'll declare a variable that I'll name value1 and I'll set its type to float64 and its value to 42.13.
Then, I'll create a new pointer that I'll name pointer1 and this time I'll use implicit typing and I'll set it using the ampersand and value1. Again, the ampersand means connect the pointer and the value to each other. Then, I'll output the value like this with fmt.Println value 1. Then, I'll use the pointer to get to the value starting with the asterisk and then the pointer name.
There's the result. I'm outputting the value. Next, I'll modify the value through the pointer. I'll start with a reference to the pointer, asterisk pointer1. Then, I'll reset its value by multiplying the pointer times another value. I'll divide the pointer value by 31. Then, I'll output the value again, once again using the pointer. Here's the results so far. I see the result, but now to prove that I'm actually affecting the underlying value, I'll change the second version to output the value directly.
I'll run the code again and I see the identical output. So, that's how pointers are used. Again, the asterisk is used when you're referencing the pointer itself and the ampersand is how you connect the value to the pointer. This is similar, in a way, to Java's reference variables but unlike in Java, the pointer doesn't have to point at any particular value and you can change it at runtime to point from one value to another. If you're used to pointers in C, C#, and similar languages, you'll find pointers in Go are pretty much the same and they're just as valuable.
- Installing Go tools
- Creating and compiling a Go workspace
- Exploring variables, constants, and types
- Storing ordered and unordered values
- Grouping related values in structs
- Programming conditional logic and loops
- Defining and calling functions
- Handling errors
- Working with files
- Creating a simple HTTP server