In today's post, we will examine some examples of passing array to function as parameter in Golang.
Arrays in Golang
Arrays are useful when planning the detailed layout of memory and sometimes can help avoid allocation, but primarily they are a building block for slices, the subject of the next section. To lay the foundation for that topic, here are a few words about arrays.
There are major differences between the ways arrays work in Go and C. In Go,
- Arrays are values. Assigning one array to another copies all the elements.
- In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it.
- The size of an array is part of its type. The typesÂ
[10]int
 andÂ[20]int
 are distinct.
Syntax
func FunctionName(parameters [size]type){
// logic code
}
We can pass one or multiple dimensional arrays to the function using the below syntax.
Example 1: A function to calculate the sum of all elements in an int array
Consider the following code, which will take an int array as argument and based on the passed arguments, it will iterate though the array and then return sum of all elements.
package main
import "fmt"
func SumArray(arr [5]int) int {
sum := 0
// iterate all elements
for k := 0; k < len(arr); k++ {
sum += arr[k]
}
return sum
}
func main() {
// initial an array
var arr = [5]int{12, 58, 7, 42, 79}
var sum int
// Passing an array as argument
sum = SumArray(arr)
fmt.Printf("Sum of the array: %d ", sum)
}
Output:
Sum of the array: 198
Note that: because the size of an array is part of its type so if the function re-write to func SumArray(arr [10]int) int {}
, the system will prompt an error.
Example 2: Passing a two-dimensional array as function parameter
In the example below, we will sum all the element inside two-dimensional array by passing it to a function.
package main
import "fmt"
func SumArray(arr [3][4]int) int {
sum := 0
// iterate all elements
for k := 0; k < len(arr); k++ {
for j := 0; j < len(arr[k]); j++ {
sum += arr[k][j]
}
}
return sum
}
func main() {
// initial array
arr := [3][4]int{
{0, 25, 1, 6},
{7, 8, 6, 7},
{8, 9, 10, 11},
}
var sum int
// Passing an array as argument
sum = SumArray(arr)
fmt.Printf("Sum of the array: %d ", sum)
}
Output:
Sum of the array: 98
Example 3: A function to modify the array's elements
In example 3, we will try to write a function that changes some elements inside an array:
package main
import "fmt"
func ChangeArray(arr [6]int) {
arr[0] = 1000
arr[1] = 1001
}
func main() {
// Creating and initializing an array
arr := [6]int{1, 2, 3, 4, 5, 6}
// Passing an array as argument
fmt.Printf("Intial array: %v\n", arr)
ChangeArray(arr)
fmt.Printf("After change: %v", arr)
}
Output:
Intial array: [1 2 3 4 5 6]
After change: [1 2 3 4 5 6]
Because we pass an array to a function, it will receive a copy of the array, not a pointer to it. So if we change the elements inside function's array, the outer one will not be affected. Instead of passing the array itself, we can pass a pointer to that array so we can modify it:
package main
import "fmt"
func ChangeArray(arr *[6]int) {
arr[0] = 1000
arr[1] = 1001
}
func main() {
// Creating and initializing an array
arr := [6]int{1, 2, 3, 4, 5, 6}
// Passing an array as argument
fmt.Printf("Intial array: %v\n", arr)
ChangeArray(&arr)
fmt.Printf("After change: %v", arr)
}
Output:
Intial array: [1 2 3 4 5 6]
After change: [1000 1001 3 4 5 6]
Summary
The above examples show how to write functions which take an array as a parameter. As explained above, you can pass arrays with any number of dimensions to a function as arguments. For more flexible usage, you and use slices instead of using arrays.
References
https://go.dev/doc/effective_go#arrays
Passing array to a function in Go - Stack Overflow