To create a pointer to an int in Go, you can use the "new" keyword or the "&" operator.
Using "new" keyword:
main.go28 chars3 lines
Using "&" operator:
main.go40 chars4 lines
In the first example, new(int)
allocates memory for an int and returns a pointer to it. This pointer is then assigned to the ptr
variable.
In the second example, &num
gets the memory address of the num
variable, which is of type int
. This memory address is then assigned to the ptr
variable.
Both methods will create a pointer to an int that you can use to manipulate the value of the int directly in memory.
gistlibby LogSnag