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 efficiently, you often need to pass arrays to functions. In this article, we will learn how to pass arrays to functions in Golang .
For example:
package main import "fmt" // Hàm tính trung bình của một mảng func calculateAverage(arr [6]int, size int) int { var sum int for _, value := range arr { sum += value } return sum / size } func main() { scores := [6]int{67, 59, 29, 35, 4, 34} average := calculateAverage(scores, len(scores)) fmt.Printf("%dn", average) }
Result:
38
Detailed explanation of how to pass array to function in Golang
In this example, the calculateAverage() function takes an array of integers and its size as arguments. It calculates the average of the array elements and returns that value. The main function initializes an array named scores and calls the function that calculates the average.
Key points:
Array declaration:
Use the syntax var arrayName [size]type to declare an array.
var numbers [5]int // Một mảng gồm 5 số nguyên
Passing arrays:
In Go, arrays are passed to functions by value, meaning the function receives a copy of the array. Changes made inside the function do not affect the original array.
Pointer to modify the original array:
If you want to modify the original array, you should pass a pointer to it.
func modifyArray(arr *[5]int) { for i := range arr { arr[i] += 10 // Tăng mỗi phần tử lên 10 } }
Modifying an array in Golang
For example:
package main import "fmt" // Hàm tăng mỗi phần tử của mảng này func incrementArray(arr *[5]int) { for i := range arr { arr[i]++ // Increment each element by 1 } } func main() { values := [5]int{1, 2, 3, 4, 5} // Chỉnh sửa mảng incrementArray(&values) fmt.Println("Incremented array:", values) }
Result:
Mảng tăng dần: [2 3 4 5 6]
Passing arrays to functions in Go allows efficient data management. Understanding how to pass and modify arrays will help you write efficient Go programs.
You should read it
- Functions are fields in Golang
- What is GoLang? How to install GoLang on Windows 10
- Difference between Go and C++
- What is Golang? Things you need to know about Golang programming language
- What is Golang? Why should you use Golang language?
- Structural equality in Golang
- How to copy one array into another array in Golang
- Anonymous structures and fields in Golang
May be interested
- Array (Array) in JavaScriptarray object - array helps you store multiple values in a single variable. it stores a set of fixed-size ranges of elements in the same type (type). an array is used to store a data set, but it is often more useful to think of an array as a collection of variables in the same type.
- The Match function (the function searches for a specified value in an array or cell range) in Excelthe match function helps you search for a specified value in an array, a range of cells, and then returns the position of the value in that array or range.
- MODE.MULT function - The function returns a vertical array of the most common values in Excelmode.mult function: the function returns a vertical array of the most frequently occurring values, or repeating values in an array or data range. support functions from excel 2010 onwards. syntax: mode.mult ((number1, [number2], ...)
- Introduction to 2D Array - 2-dimensional array in JavaScriptin the following article, we will introduce and show you some basic operations to create and access 2-dimensional arrays - 2d array in javascript. essentially, a 2-dimensional array is a concept of a matrix of matrices - a matrix, used to store information. each 1 element contains 2 separate indexes: row (y) - column and column (x) - column.
- Functions are fields in Golangin golang, you can define functions as fields in a struct. this feature allows you to bind behavior (methods) directly to data types.
- Structural equality in Golangin go language, you are allowed to compare two structures if they are of same type and contain same field values with the help of == operator or deeplyequal() method.
- GEOMEAN function - The function returns the average of a positive array or range of data in Excelgeomean function: the function returns the average of a positive array or range of data. use the function to calculate the average growth when knowing the gross profit with variable interest rates. syntax: geomean (number1, [number2], ...)
- Arrays in Golangarrays in golang or go programming language are quite similar to other programming languages. here are the things you need to know about arrays in golang.
- How to use the Match function in Excelthe match function on excel looks for a value defined in an array, cell range on the data table and returns the position of the value in the array, that range.
- Recommended fields in Golangin go structs, fields are encouraged to be the same as anonymous fields, the type of the field is the name of the field.