golang func indefinite parameters

PHPz
Release: 2023-05-14 20:29:35
Original
344 people have browsed it

In Golang functions, parameters can be passed using indefinite parameters. Indefinite parameters refer to using a special type to receive all parameters when the number of parameters is uncertain. In a function, you can use ...type to define indefinite parameters, where type is any data type, such as int, string, etc.

The following is a basic example that uses indefinite parameters to calculate the sum of a set of numbers:

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

func main() {
    fmt.Println(sum(1, 2, 3, 4, 5)) // 输出 15
}
Copy after login

In the above example, the sum function receives an indefinite parameter nums of type int. , use a for loop to iterate through all parameters and accumulate their values. In the main function, we pass 5 int type parameters when calling the sum function and output their sum.

The advantage of using indefinite parameters is that you can easily pass any number of parameters without having to declare their number in advance. It is ideal for functions that need to handle a dynamic number of parameters, such as processing logs, file paths, URLs, etc.

In addition to receiving an indefinite number of parameters of the same type, indefinite parameters can also receive parameters of different types. The following is an example that uses indefinite parameters to output an arbitrary number of values ​​and their types:

func display(values ...interface{}) {
    for _, value := range values {
        fmt.Printf("%v (%T)
", value, value)
    }
}

func main() {
    display(1, "hello", true)
}
Copy after login

In the above example, the display function receives an indefinite number of parameter values ​​of type interface{}, using for Loop through all parameters and print out their values ​​and types using the Printf function. In the main function, when we call the display function, we pass an int type value 1, a string type value "hello" and a bool type value true, and output their values ​​and types.

It should be noted that when using an indefinite parameter, it must be the last parameter in the function parameter list. Because arguments must be passed in a deterministic order when a function is called, variable arguments must be at the end of the argument list so that the other arguments are already deterministic.

In short, using indefinite parameters is a very convenient way to handle a dynamic number of parameters, which allows us to write flexible functions more easily. In Golang, indefinite parameters are very easy to use. You only need to use ...type when defining a function.

The above is the detailed content of golang func indefinite parameters. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!