Pass different types of arguments in variadic function
In the following example, the function signature accepts an arbitrary number of arguments of type slice.
Example
package main
import (
"fmt"
"reflect"
)
func main() {
variadicExample(1, "red", true, 10.5, []string{"foo", "bar", "baz"},
map[string]int{"apple": 23, "tomato": 13})
}
func variadicExample(i ...interface{}) {
for _, v := range i {
fmt.Println(v, "--", reflect.ValueOf(v).Kind())
}
}
Output
1 -- int
red -- string
true -- bool
10.5 -- float64
[foo bar baz] -- slice
map[apple:23 tomato:13] -- map
Most Helpful This Week
How to kill execution of goroutine?
Find out how many logical processors used by current process
How to replace emoji characters in string using regex in Golang?
How to fetch an Integer variable as String in Go?
Example to create custom error
How to declare and access pointer variable?
How to use function from another file golang?
Golang String Concatenation
How to split a string on white-space?
How to check UPPERCASE characters in a string in Golang?