Prerequisite: Pointers in Go
Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. A pointer is a special variable so it can point to a variable of any type even to a pointer. Basically, this looks like a chain of pointers. When we define a pointer to pointer then the first pointer is used to store the address of the second pointer. This concept is sometimes termed as Double Pointers.
How to Declare a pointer to pointer in Golang?
Declaring Pointer to Pointer is similar to declaring pointer in Go. The difference is we have to place an additional ‘*’ before the name of pointer name. This is generally done when we are declaring the pointer variable using the var keyword along with the type. Below example and image will explain the concept in much better way.
Example 1: In the below program the pointer pt2 stores the address of pt1 pointer. Dereferencing pt2 i.e. *pt2 will give the address of variable v or you can also say the value of pointer pt1. If you will try **pt2 then this will give the value of the variable v i.e. 100.
The Value of Variable V is = 100 Address of variable V is = 0x414020 The Value of pt1 is = 0x414020 Address of pt1 is = 0x40c128 The value of pt2 is = 0x40c128 Value at the address of pt2 is or *pt2 = 0x414020 *(Value at the address of pt2 is) or **pt2 = 100
Example 2: Let’s make some changes in the above program. Assigning some new value to the pointers by changing the value of pointers using dereferencing as shown below:
The Value of Variable v is = 100 Value stored in v after changing pt1 = 200 Value stored in v after changing pt2 = 300
- Returning Pointer from a Function in Go
- Pointer to a Struct in Golang
- How to find the length of the pointer in Golang?
- How to find the capacity of the pointer in Golang?
- Golang Pointer to an Array as Function Argument
- Higher-Order Function in Golang
- Hello World Program : First program while learning Programming
- Golang Tutorial - Learn Go Programming Language
- Packages in Golang
- How to Install Golang on MacOS?
- Golang | Deadlock and Default Case in Select Statement
- Rune in Golang
- Encapsulation in Golang
- How to Map a Rune to the Specified Case in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai