![]() |
VOOZH | about |
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:
38
In this example, the calculateAverage() function takes an array of integers and its size as arguments. It computes the average of the array elements and returns it. The main function initializes an array called scores and calls the average calculation function.
var arrayName [size]type to declare an array.var numbers [5]int // An array of 5 integersfunc modifyArray(arr *[5]int) {
for i := range arr {
arr[i] += 10 // Increment each element by 10
}
}
Incremented array: [2 3 4 5 6]
Passing arrays to functions in Go allows for efficient data management. Understanding how to pass and modify arrays will help you write effective Go programs.