To pass a pointer to a function in go, you simply need to declare the function parameter as a pointer by using the '*' symbol. Here is an example function that takes a pointer to an int as a parameter:
main.go118 chars5 lines
To call this function with a pointer to an int, you first need to declare the int variable and then get its memory address using the '&' symbol:
main.go55 chars5 lines
This will output Value of the pointer: 42
. In this example, &myInt
is used to obtain a pointer to myInt
, which is then passed to myFunction
. Inside the function, the pointer is dereferenced using *
to get the value of the int it points to.
gistlibby LogSnag