Table of contents
1.
Introduction
2.
The Need for Returning Pointer 
3.
Implementation
3.1.
Example 1
3.2.
Output
3.3.
Example 2
3.4.
 Output
4.
Frequently Asked Questions
4.1.
Are Function Pointers required in Go?
4.2.
What makes Golang so fast?
4.3.
Is Go a frontend or backend language?
4.4.
How can we run a Go program?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Returning Pointer from a Function in Go

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

While writing a program in Golang (also known as Go), you will write functions and methods and pass data to these functions as arguments. In some cases, you might want to tell the function where the data is located instead of sending the actual data to the function. That is where a Pointer can be really useful for you. A Pointer data type holds the memory address of the data instead of the data itself. We can give pointers to the function and return pointers from a function in Go.

We will understand the need for returning pointer from a function and how we can implement returning pointers in Go. We have also covered Pointers to an Array as Function Arguments in Go, So check that out after going through this article.

The Need for Returning Pointer 

When it comes to deciding return from a function while writing a software program, memory usage and efficiency are crucial deciding factors. If we design a function in Golang to return a pointer, there will be an escape, and the resulting variable will be allocated on the heap. Meanwhile, the return will be allocated to the stack if the function returns a value. In terms of memory, Stacks are cheap, and Heaps are expensive (If you are not familiar with Stack and Heap allocations, you can check out our articles like Heap Allocation and Stack Allocation). So why would we need to return a pointer from a function in the first place?

The need of returning a pointer from a function depends on the structure and usage of the business scenario or project. Generally, while returning a resource, it is recommended to return a pointer to a large structure rather than the data itself. 

In addition, as soon as the function exits, the function slack is also removed, causing the function's local variable to go out of scope. The Go compiler will allocate this variable on the heap instead of assigning the memory on the stack to the function's local variable. We will look at this implementation, known as escape analysis, in the next section of this article.

Implementation

In the program below, we will write a code in Golang that will return a pointer. Here the variable message will have the memory allocated on the heap as the Go compiler will perform an escape analysis to escape the variable from the local scope.

In simple terms, we will create a function called display() that will return a pointer instead of a conventional variable.

func display() *string {

  message := "Ninja Coder"

  // returns the address of message
  return &message

}

 

Here the *string suggests that the function will return a string type pointer. Also, the return &message statement indicates that the function returns the location or address of the message variable to the main() function. Our complete code looks something like this:

Example 1

// pointer_return.go

package main
import "fmt"
 
func main() {

  // function call
  result := display()
  fmt.Println("Be a ", *result)

}

func display() *string {

  message := "Ninja Coder"


  return &message

}

Output

The return address is assigned to the result pointer, and to get the value stored in that memory address, we use the code *result.

Below is another example of using an int-type pointer instead of a string type. The code and the implementation here are precisely similar to our first example, so it should be pretty easy to understand the variation of the data type with the *int statement.

Example 2

// pointer_return.go

package main
import "fmt"
 
func main() {
 
    // function call
    n := rpf()
    fmt.Println("Value of n is: ", *n)
 
}
 
func rpf() *int {
 
    lv := 123
 
    return &lv

 Output

Frequently Asked Questions

Are Function Pointers required in Go?

The Golang programming language avoids the function pointers explicitly to make the language as simple as possible. That is because function pointers can sometimes get complicated, as seen in other programming languages.

 

What makes Golang so fast?

Golang naturally outperforms languages having virtual runtimes or are interpreted because it is compiled to machine code. Go programs compile extremely fast, and the resulting binary is also minimal.

 

Is Go a frontend or backend language?

Go is generally preferred as a backend language as it offers higher performance for developing concurrent applications. 

 

How can we run a Go program?

To run a go program, You first need to create a file with a .go extension and write your golang code in it. Then you can run the program from the command prompt by navigating to your program directory and running the following command.

go run HelloWorld.go

Conclusion

In this article, we have extensively discussed the implementation, requirement, and concept behind returning pointers from a function in Golang with the help of examples. There are many more features and implementations available in Golang that we are yet to discuss.

We hope that this blog has helped you enhance your knowledge of Golang and if you would like to learn more, check out our articles on Introduction to Go and Exploring the Dynamics of Golang. Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass