From the course: Learning Go
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
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 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. I'll start in this file by creating a new variable that I'll call p for pointer, and I'll declare the type but not assign an initial value. This will be an integer, but instead of a simple integer, I'll prefix this with an asterisk. This is called the dereferencing operator, and it means that it's not going to be the type itself. Instead, this variable is going to point to a variable of that type. Next in my Println, I'll change this label to Value of p, and then I'll pass in the variable p. Now if I run this code, I'll get the output value of p nil in brackets, but that's not actually how you're supposed to reference a pointer variable. Instead, you want to include that same dereferencing operator, the asterisk, and right now, the p variable doesn't…