In Go language, array is a basic data type. Its underlying implementation is a fixed-length continuous memory area. The array is defined as follows:
var arr [5]int // 定义一个长度为5的int类型的数组arr
In actual development, we sometimes You need to convert an array into a variable parameter, that is, pass each element in the array to a function. In this article, we will learn how to convert an array into a variadic argument.
In Go language, you can use the ...
operator to convert an array into a variable parameter. The example is as follows:
package main import "fmt" func test(args ...int) { fmt.Println(args) } func main() { arr := [5]int{1, 2, 3, 4, 5} test(arr[:]...) // 将数组arr转换为变参 }
The result output is:
[1 2 3 4 5]
In the above code, we convert the array arr
into a variable parameter by slicing [:]
, and then Passed to function test
. In function test
, we can get each element in the array by traversing the variable parameters. It should be noted that when using the ...
operator to convert an array into a variable parameter, you need to pay attention to the following points:
...
operator must follow the array and directly follow [:]
. ...
operator when passing. In actual development, the application scenarios of converting arrays into variable parameters are very wide. Below we introduce two common application scenarios.
When calling the function, we can directly pass the array as the parameter of the function to the variable parameter. For example:
package main import "fmt" func sum(nums ...int) { total := 0 for _, num := range nums { total += num } fmt.Println(total) } func main() { arr := [5]int{1, 2, 3, 4, 5} sum(arr[:]...) }
In the above code, we define a sum
function that receives any number of integer variables and adds them together. In the main
function, we define an array arr
with a length of 5, and convert it into a variable parameter and pass it to the sum
function. In the function, each element in the array is obtained by traversing the variable parameters, and they are accumulated and summed.
Sometimes, we need to copy all or part of the elements in an array to another array. In this case, the original array can be converted to Variable parameters, and then use the append
function to add the elements of the variable parameters to the new array. For example:
package main import "fmt" func main() { arr1 := [5]int{1, 2, 3, 4, 5} arr2 := []int{6, 7, 8, 9, 10} arr2 = append(arr2, arr1[:]...) // 将数组arr1中的元素添加到切片arr2中 fmt.Println(arr2) }
In the above code, we define two arrays arr1
and arr2
, and convert the array arr1
into variable parameters , use the append
function to add the elements in the variable parameters to the end of the array arr2
. It should be noted that when using the append
function to add a slice or array, you need to use the ...
operator at the end of the slice or array to expand the variable parameters.
Through the study of this article, we have learned how to convert arrays into variable parameters, and introduced two common application scenarios for converting arrays into variable parameters in actual development. It should be noted that when using the ...
operator to convert an array into a variable parameter, you need to pay attention to the syntax rules and details to avoid errors.
The above is the detailed content of golang array conversion parameter. For more information, please follow other related articles on the PHP Chinese website!