Table of contents
1.
Introduction
2.
What is Pointer to a Function in Go?
2.1.
Passing the Pointer to the Function
2.1.1.
Output
2.2.
Pass the Address of the Variable
2.2.1.
Output
3.
Implicit Definition
3.1.
Example of Passing Pointer to a Function
3.1.1.
Output
4.
Frequently Asked Questions
4.1.
How may a pointer be passed to a function in Go?
4.2.
Do function pointers exist in Go?
4.3.
How does a pointer look in Go?
4.4.
In Golang, how can one declare a pointer variable?
4.5.
Is pass-by reference supported in Go?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Pointers to a Function in Go

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Pointers to a Function in Go

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

Output of Passing Pointer to Function

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

Output of Passing Address of Variable

Implicit Definition

Go programming language implicitly supports function pointers or pointers to a function in Go since it allows us to store and call functions with other variables. The support for first-class functions must be present.

Example of Passing Pointer to a Function

In Go programming, a pointer can be passed to a function. Simply specify the function parameter as a pointer type to do this.

In the example below, we call a function with two pointers and change its internal value, which is reflected in the caller function.

/* 
	Example of pointers to a function in Go
*/

package main
import "fmt"

func main() {
	// Creating variables 
	var p int = 100
	var q int = 200
	
	fmt.Printf("Actual P is : %d\n", p )
	fmt.Printf("Actual Q is : %d\n", q )
	
	/* 
		Changing the values by invoking a function.
		The symbols &p and &q denote a pointer to the 
		variable p's address, respectively, and the 
		variable q's address, respectively.
	*/
	swap(&p, &q);
	
	fmt.Printf("Changed P is : %d\n", p )
	fmt.Printf("Changed Q is : %d\n", q )
}

func swap(i *int, j *int) {
	var temp int
	
	// Save the value at address i *
	temp = *i   
	
	// Put j into i 
	*i = *j 
	
	// Put temp into j
	*j = temp    
}

Output

Output of pointers to a function

Frequently Asked Questions

How may a pointer be passed to a function in Go?

In Go programming, a pointer can be passed to a function. Simply specify the function parameter as a pointer type to do this.

Do function pointers exist in Go?

The memory addresses of variables are stored in Go pointers. Additionally, we can provide function pointers just like regular variables.

How does a pointer look in Go?

Go's pointer representation is the * (asterisk) character followed by the type of the stored value.

In Golang, how can one declare a pointer variable?

The character * used to declare pointer variables and access the value contained in the address. It is also known as the dereferencing operator. The & operator, often known as the address operator, is used to obtain a variable's address from a pointer or to return the address of a variable.

Is pass-by reference supported in Go?

Whether Go composite types are provided to functions via reference is debatable. Go does explicitly not in any manner support the "Pass-By-Reference" meaning. The reason is straightforward: unlike other programming languages like C++, Go does not provide reference variables.

Conclusion

This article discusses the pointers to a function in Go programming language. We have discussed its definition, its uses, and how we can implement it with the help of simple examples. 

We hope this blog has helped you enhance your knowledge regarding pointers to a function in Go. Check out our articles on Introduction to GoGo Pointers & Methods, and Returning Pointer from a Function in Go to learn more.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; look at the Top 150 Interview Puzzles interview experiences, and interview bundle for placement preparations.

Thank you

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja!

Live masterclass