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
Example to handle GET and POST request in Golang
Different ways for Integer to String Conversions
How to check string contains uppercase lowercase character in Golang?
Replace first occurrence of string using Regexp
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]
How to get first and last element of slice in Golang?