Golang Pointer to an Array as Function Argument Last Updated : 22 Jul, 2019 Comments Improve Suggest changes Like Article Like Report Prerequisite: Pointers in Golang Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Whereas an array is a fixed-length sequence which is used to store homogeneous elements in the memory. You can use the pointers to an array and pass that one as an argument to the function. To understand this concept let's take an example. In the below program we are taking a pointer array arr which has 5 elements. We want to update this array using a function. Means modifying the array(here the array is {78, 89, 45, 56, 14}) inside the function and that will reflect at the caller. So here we have taken function updatearray which has pointer to array as argument type. Using updatearray(&arr) line of code we are passing the address of the array. Inside function (*funarr)[4] = 750 or funarr[4] = 750 line of code is using dereferencing concept to assign new value to the array which will reflect in the original array. Finally, the program will give output [78 89 45 56 750]. C // Golang program to pass a pointer to an // array as an argument to the function package main import "fmt" // taking a function func updatearray(funarr *[5]int) { // updating the array value // at specified index (*funarr)[4] = 750 // you can also write // the above line of code // funarr[4] = 750 } // Main Function func main() { // Taking an pointer to an array arr := [5]int{78, 89, 45, 56, 14} // passing pointer to an array // to function updatearray updatearray(&arr) // array after updating fmt.Println(arr) } Output: [78 89 45 56 750] Note: In Golang it is not recommended to use Pointer to an Array as an Argument to Function as the code become difficult to read. Also, it is not considered a good way to achieve this concept. To achieve this you can use slice instead of passing pointers. Example: C // Golang program to illustrate the // concept of passing a pointer to an // array as an argument to the function // using a slice package main import "fmt" // taking a function func updateslice(funarr []int) { // updating the value // at specified index funarr[4] = 750 } // Main Function func main() { // Taking an slice s := [5]int{78, 89, 45, 56, 14} // passing slice to the // function updateslice updateslice(s[:]) // displaying the result fmt.Println(s) } Output: [78 89 45 56 750] Comment More infoAdvertise with us Next Article Golang Pointer to an Array as Function Argument A anshul_aggarwal Follow Improve Article Tags : Go Language Golang-Pointers Golang Similar Reads How to pass an Array to a Function in Golang? In Go, arrays are used to store a fixed-length collection of data of the same type. To manage this data effectively, you may often need to pass arrays to functions. In this article we will learn "How to pass an Array to a Function in Golang".Example:Gopackage main import "fmt" // Function to calcula 2 min read Function Arguments in Golang In Golang, functions are groups of statements used to perform tasks, with optional returns. Go supports two main ways to pass arguments: Pass by Value and Pass by Reference. By default, Go uses pass-by-value.Basic Terms in Parameter Passing to Functions:Actual Parameters: The arguments passed to a f 2 min read How to Copy an Array into Another Array in Golang? In Go, an array is a fixed-length sequence that holds elements of a specific type. Unlike slices, arrays have a constant size, which is determined when the array is declared. Copying one array to another is straightforward but requires that both arrays have the same length and type.Examplepackage ma 3 min read Passing Pointers to a Function in Go Prerequisite: Pointers in Go Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. You can also pass the pointers to the function like the variables. There are two ways to do this as follows: Create a pointer and simply pass it to 3 min read reflect.ArrayOf() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.ArrayOf() Function in Golang is used to get the array type with the given count and element type, i.e., if x repr 2 min read Like