Basic syntax
In Python, when the number of function parameters is uncertain, you can dynamically obtain parameters in the function using the following method. args is essentially a list, and kwargs is a dict
def myFun(*args, **kwargs):
In Go, there are similar implementation methods, but in Go, they can only implement array methods similar to *args, but they cannot implement **kwargs. Implementing this method is actually a three-point expression method of arrays. Let's recall it here.
Description of Ellipsis on three points (…)
We often see this method in Go. First of all, the three points are Ellipsis in English, which is translated into Chinese as "omitted". It may be easier for you to understand the function of the three points when you see this word. It has different functions at different locations. For example, in the above-mentioned array definition, the declaration of array length is omitted, but is determined based on the array initialization value. In the function definition, we will see similar usage methods, and we will explain them in detail.
In fact, the essential expression of the three points is to use the feature of arrays to implement variable parameters. Let’s take a look at the definition format:
// arg will be [...]int func myfunc(arg ...int) {} // paras will be [...]string func myfunc(arg, paras ... string) {}
Example 1: Get variable parameters in function
Loop to get mutable parameters and pass some arguments into subfunctions
package main import "fmt" func myfunc(arg ... string) { ("arg type is %T\n", arg) for index, value := range arg { ("And the index is: %d\n", index) ("And the value is: %v\n", value) } } func main() { myfunc("1st", "2nd", "3rd") }
Analysis of the above example:
Variable parameter arg type is []string
Loop through for and get the value
arg type is []string
And the index is: 0
And the value is: 1st
And the index is: 1
And the value is: 2nd
And the index is: 2
And the value is: 3rd
Example 2: Passing slices to variable parameters
We implement a new function mySubFunc based on the above program, and try to pass a slice to the function
package main import "fmt" func myfunc(arg ... string) { ("arg type is %T\n", arg) for index, value := range arg { ("And the index is: %d\n", index) ("And the value is: %v\n", value) } // Call sub funcation with arguments ("Pass arguments: %v to mySubFunc\n", arg[1:]) mySubFunc(arg[1:] ...) } func mySubFunc(arg ... string) { for index, value := range arg { ("SubFunc: And the index is: %d\n", index) ("SubFunc: And the value is: %v\n", value) } } func main() { myfunc("1st", "2nd", "3rd") }
Let's analyze this code:
1. Most of the logic is the same as the above code. Here we use slice arg[1:] to get the values of some variable parameters.
2. When transferring to the subfunction mySubFunc(), this expression is used mySubFunc(arg[1:]…). Here is a description of the usage of slices.
… signifies both pack and unpack operator but if three dots are in the tail position, it will unpack a slice.
Three points at the end will unpack a slice
Example 3: Multiple parameters
Let's look at a multi-parameter example
package main import "fmt" func myfunc(num int, arg ... int) { ("num is %v\n", num) for _, value := range arg { ("arg value is: %d\n", value) } } func main() { myfunc(1, 2, 3) }
Let's analyze this code:
One function parameter is the integer variable num, and the mutable variable arg
The first parameter in the main function is num, and the following is stored in arg
So the output is as follows
num is 1
arg value is: 2
arg value is: 3
This is the end of this article about the detailed explanation of Variadic Parameters in Go. For more related contents of Variadic Parameters in Go, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!