pass a pointer to a function with a parameter of type int in go

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.go
func myFunction(ptr *int) {
    // do something with the pointer
    fmt.Printf("Value of the pointer: %d\n", *ptr)
}
118 chars
5 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.go
func main() {
    myInt := 42
    myFunction(&myInt)
}
55 chars
5 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