variables, pointers, memory location


Situation: For some reason, you need to access the address (or memory location) of variable i.

Solution:
package main

import "fmt"

func main() {
    var i int
    fmt.Println("address of i is:", &i, "value at address is ", *&i)

    x := 23
    fmt.Println("address of x is:", &x, "value at address is ", *&x)
}

Running go run a.go, I get...
address of i is: 0x18600110 value at address is  0
address of x is: 0x18600128 value at address is  23

No comments:

Post a Comment