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