Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Using a Pointer as Function Argument
3.
Pointer to an Array as Function Argument
3.1.
Program
3.2.
Output
4.
Slice Implementation
4.1.
Program  
4.2.
Output
5.
Frequently Asked Questions
5.1.
Are Function Pointers required in Go?
5.2.
What makes Golang so fast?
5.3.
Is Go a frontend or backend language?
5.4.
How can we run a Go program?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Pointer to an Array as Function Argument in Go

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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 you might want to pass pointers as an argument to the function. 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 use of pointer to an array as function arguments and how we can implement it in Go. We have also covered Returning Pointer from a Function in Go, So check that out after going through this article.

Using a Pointer as Function Argument

Golang allows you to pass a Pointer as a Function argument. That comes in handy because the method set of a value type is a subset of the method set of its associated pointer type. That might be a little difficult to understand straightaway. So let us look at an example to drive the point home. 

Let us assume that you have the following structure.

type Example struct {
    Name string
}

And then, you define two methods as follows:

func (w *Example) Foo() {
    ...
}


func (w Example) Bar() {
    ...
}

Here, the type *Example has both the Foo() and Bar() methods, while the type Example has only the method Bar(). Meaning if you have an interface like this:

type Newinf interface {
    Foo()
    Bar()
}

Then *Example will implement Newinf, but Example won't as it lacks the method Foo(). Thus, using pointers as a function argument becomes relevant while designing software. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Pointer to an Array as Function Argument

In very elementary terms, you can create a pointer to an array and then pass that pointer as a Function in Golang. Doing so will let you update or modify the array using its reference and reflect the changes in the original array. To understand this concept better, we will look at an implementation of using a pointer to an array as a function argument. 

Here, we will take a pointer to a five-element array and update this array using a function. The function updatearr has a pointer to the array as an argument. 

func updatearr(funarr *[5]int) {
 }

We will use the dereferencing concept inside the function to assign a new value to the array, which reflects in the value of the original value.

Program


package main
import "fmt"

func updatearr(funarr *[5]int) {
 
    // dereferencing the pointer
    (*funarr)[4] = 123
     
}
 
func main() {
 
    // Taking an pointer to an array
    arr := [5]int{10, 11, 22, 33, 44}
 
    // passing pointer to an array
    // to function updatearr
    updatearr(&arr)
 
    // updated array
    fmt.Println(arr)
}

When we change the value of *funarr inside the updatearr() function, the original value of arr inside the main() function also changes.

Output

We observe the change in the value of arr because funarr and arr refer to the same address in the memory. So, updating one updates the other.

Slice Implementation

Although this implementation of passing a pointer to an array as an argument to a function and modifying it works, it is not the ideal or the recommended approach to achieve this in Go. You can use Slice instead of pointers. Slices are similar to arrays but more powerful and flexible. You can read more about Go Slice here

We will implement the same program as above, passing Slice instead of passing a pointer to an array.

Program  


package main
import "fmt"

func updateslice(funarr []int) {
 
    // updating value at specified index
    funarr[4] = 123
     
}
 
func main() {
 
    // Taking slice
    s := [5]int{10, 11, 22, 33, 44}
 
    // passing slice    
    // to function updatearr
    updateslice(s[:])
 
    // updated array
    fmt.Println(s)
}

This implementation gives the same output with a much more efficient and cleaner code design.

Output

 

Read Also -  Difference between argument and parameter

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, concept, and usage of passing a pointer to an array as a function argument in Golang with the help of examples. There are many more features and implementations available in Golang that we are yet to discuss.

Recommended problems -

 

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!

Previous article
Returning Pointer from a Function in Go
Next article
Pointer to Struct in Go
Live masterclass