I want to convert a pointer *int
to its real value int
, in Go language.
How do you do it?
Just use the *
operator. For example:
var i int = 10 // `i` is an integer, with value 10
var p *int = &i // `p` is a pointer to an integer, its value is a memory address
var n int = *p // `n` is again an integer, with value 10
Once you get the hang of what's happening, the above code can be written in a more idiomatic (and simpler) way like this, assuming that we're inside a function:
i := 10
p := &i
n := *p