How pointer & and * and ** works in Golang?
The below program is curry of pointers. The value of intVar is equals to value of **pointerToPointerVar.
Example
package main
import "fmt"
func main() {
var intVar int
var pointerVar *int
var pointerToPointerVar **int
intVar = 100
pointerVar = &intVar
pointerToPointerVar = &pointerVar
fmt.Println("\n")
fmt.Println("intVar:\t\t\t", intVar)
fmt.Println("pointerVar:\t\t", pointerVar)
fmt.Println("pointerToPointerVar:\t", pointerToPointerVar)
fmt.Println("\n")
fmt.Println("&intVar:\t\t", &intVar)
fmt.Println("&pointerVar:\t\t", &pointerVar)
fmt.Println("&pointerToPointerVar:\t", &pointerToPointerVar)
fmt.Println("\n")
fmt.Println("*pointerVar:\t\t", *pointerVar)
fmt.Println("*pointerToPointerVar:\t", *pointerToPointerVar)
fmt.Println("**pointerToPointerVar:\t", **pointerToPointerVar)
}
Output
intVar: 100
pointerVar: 0x1142214c
pointerToPointerVar: 0x114240e0
&intVar: 0x1142214c
&pointerVar: 0x114240e0
&pointerToPointerVar: 0x114240e8
*pointerVar: 100
*pointerToPointerVar: 0x1142214c
**pointerToPointerVar: 100
Most Helpful This Week
Regular expression to extract domain from URL
Simple example of Map initialization in Go
Split a string at uppercase letters using regular expression in Golang
What is Rune? How to get ASCII value of any character in Go?
How to read current directory using Readdir?
How to check lowercase characters in a string in Golang?