create a pointer to an int in go

To create a pointer to an int in Go, you can use the "new" keyword or the "&" operator.

Using "new" keyword:

main.go
var ptr *int
ptr = new(int)
28 chars
3 lines

Using "&" operator:

main.go
var ptr *int
var num int = 5
ptr = &num
40 chars
4 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