Address operators
For an operand x of type T, the address operation &x generates a
pointer of type *T to x. The operand must be addressable, that is,
either a variable, pointer indirection, or slice indexing operation;
or a field selector of an addressable struct operand; or an array
indexing operation of an addressable array. As an exception to the
addressability requirement, x may also be a composite literal.
Primitive integer types are not addressable. If the integer is a register variable, there is no memory address. If the integer is a constant, providing its memory address would allow it to be modified.
Therefore, create an integer type variable and take its address or use an integer pointer type. For example,
package main
import "fmt"
func main() {
var i int = 42
var pi = &i
j := int(7)
pj := &j
var pk = new(int)
k := *pk
fmt.Println(i, pi)
fmt.Println(j, pj)
fmt.Println(k, pk)
}
Output:
42 0xf840043100
7 0xf840043108
0 0xf840043110