Home >Backend Development >Golang >golang variable parameters

golang variable parameters

WBOY
WBOYOriginal
2023-05-16 16:22:38676browse

The variable parameter function in Golang means that the function can accept different numbers of parameters. When defining a function, you can use three dots "..." to indicate that the number of parameters is not fixed, and these parameters will be packed into a slice.

In Golang, there can only be one variable parameter in the parameter list of a function, which is usually placed at the end of the parameter list.

For example, the following is a function that calculates the sum of any number of integers:

func sum(nums ...int) int {
    total := 0
    for _, num := range nums {
        total += num
    }
    return total
}

In the function signature, three dots "..." are used to indicate indefinite parameters, parameter names is "nums", the data type is "int", so "nums" is actually a slice of integers.

When calling this function, you can pass any number of integers as parameters, and there is no need to specify the number of parameters:

fmt.Println(sum(1, 2, 3))      // 输出 6
fmt.Println(sum(10, 20, 30, 40)) // 输出 100

Indefinite parameter functions have strong flexibility and can handle a variety of Different types and numbers of parameters. The following is an example for calculating the average of two or three floating point numbers:

func avg(nums ...float64) float64 {
    sum := 0.0
    for _, num := range nums {
        sum += num
    }
    return sum / float64(len(nums))
}

fmt.Println(avg(1.0, 2.0))         // 输出 1.5
fmt.Println(avg(1.0, 2.0, 3.0))    // 输出 2.0

It should be noted that in the Go language, variable parameters must also follow the order of parameter passing when calling functions. For example, in the following example, if the parameters are reversed, the function will report an error:

func foo(num int, strs ...string) {
    fmt.Println(num)
    fmt.Println(strs)
}

foo("hello", "world", 1)  // 报错!第一个参数应该是整数类型

Inside the function, you can use a for loop to iterate the indefinite parameters like operating a slice, and perform an operation on each element in it. deal with. After processing the variable parameters, you can also use the len() function to obtain the number of variable parameters.

In short, in Golang, variable parameter functions can greatly improve the flexibility and practicality of the program, and are very convenient and fast for processing uncertain numbers and types of parameters.

The above is the detailed content of golang variable parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:golang inheritance methodNext article:golang inheritance method