Introduction
A function is a collection of sentences or statements that work together to complete a goal. Each Go program has at least one main function (). Your code can be broken up into different functions.
The function may need to change the data in the first variable. For instance, when the bank customer deposits money into their account, you want the deposit function to access the actual balance. In this instance, you only need to inform the function of the data's location in memory rather than sending the information itself.

A pointer data type stores the memory address of the data but not the actual data. In pointers to a function in Go, the function is informed of the location of the data in memory but not its value by the memory address. Instead of giving the data to the function, you can give it a pointer, which will change the original variable accordingly. Since only the variable's location is supplied to the function, it is known as passing by reference.
What is Pointer to a Function in Go?
The address of the executable code for a function is indicated by a function pointer or pointers to a function in Go. Pointers are helpful for both calling functions and passing them as parameters to other functions.
There may be a pointer in some programming languages, such as C/C++, that can store the address of a function and then call it later using the address stored, known as a function pointer. Due to design choices emphasizing simplicity, GO does not support function pointers.
In the Go programming language, often known as Golang, pointers are variables used to store the memory address of other variables. Like variables, pointers can also be passed to a function. There are two methods for doing this:
- Make a pointer, then pass it directly to the function.
- Pass a variable's address to the function call.
Passing the Pointer to the Function
In this method, we will make a pointer, then pass it directly to the function. The function ‘pointerToFun’ is used in the program below. It has an integer type pointer parameter that tells the function only to accept arguments of the pointer type. This function essentially modified the value of the variable ‘p’. ‘p’ starts with a value of 300. However, the value changed to 654 after the function was called, as seen in the output.
/*
Example of pointers to a function in Go create a pointer in the Go program, then send it to the function.
*/
package main
import "fmt"
// Using an integer-type pointer function as a parameter
func pointerToFun(x * int)
{
// Dereferencing
* x = 654
}
// Function main
func main()
{
// Using a typical variable
var p = 300
fmt.Printf("Actual value of p before function call : %d\n", p)
// Using a pointer variable and giving it p's address
var z * int = & p
// Calling the function while passing the function's pointer
pointerToFun(z)
fmt.Printf("Changed value of p after function call : %d\n", p)
}
Output

Pass the Address of the Variable
In this method, we will pass a variable's address to the function call. Unlike ‘z’(pointer variable) in the program above, we are not establishing a pointer to store the address of the variable ‘p’. Similar to the previously mentioned procedure, we are directly sending the address of ‘p’ to the function call.
/*
Example of pointers to a function in Go create a pointer in the Go program by giving the function the variable's address.
*/
package main
import "fmt"
// Using an integer-type pointer function as a parameter
func pointerToFun(x *int)
{
// Dereferencing
*x = 654
}
// Function main
func main()
{
// Using a typical variable
var p = 300
fmt.Printf("Actual value of p before function call : %d\n", p)
// Invoking the function while supplying the variable p's address
pointerToFun(& p)
fmt.Printf("Changed value of p before function call : %d\n", p)
}
Output
