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!