Home > Backend Development > Golang > golang array conversion parameter

golang array conversion parameter

WBOY
Release: 2023-05-11 10:41:36
Original
636 people have browsed it

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
Copy after login

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.

Basic steps for converting array parameters

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转换为变参
}
Copy after login

The result output is:

[1 2 3 4 5]
Copy after login

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:

  1. The array must be an ordinary array, not a slice, etc. other types. The
  2. ... operator must follow the array and directly follow [:].
  3. You need to use the ... operator when passing.

Application scenarios of array conversion parameters

In actual development, the application scenarios of converting arrays into variable parameters are very wide. Below we introduce two common application scenarios.

1. Use the array as the parameter of the function

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[:]...)
}
Copy after login

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.

2. Copy array elements to another array

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)
}
Copy after login

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.

Summary

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template