This is a creation in Article, where the information may have evolved or changed.
1, fixed type of indefinite parameters
Syntax format: ArgumentName ... Type
Ackage Main
Import "FMT"
Func F1 (args ... int) {
For _, V: = Range args {
Fmt. Println (v)
}
}
Func Main () {
F1 ()
F1 (1)
F1 (3, 5)
}
2, any type of indefinite parameters
interface{}: null interface type because any type implements an empty interface, so any type of object can be assigned to an empty interface.
Package Main
Import "FMT"
Func F1 (args ... interface{}) {
For _, V: = Range args {
Fmt. Println (v. (int))//The interface type assertion is used here
}
}
Func Main () {
F1 ()
F1 (1)
F1 (3, 5)
}
3, the transfer of indefinite parameters
Assuming that args is an indeterminate parameter, you can pass the argument to another function with an indeterminate argument
Func F1 (args ... int) {
F2 (args ...)
F2 (args[1:] ...)
}
Func F2 (args ... int) {
//
}
173 Reads