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
Golang String Concatenation
Select single argument from all arguments of variadic function
Example: How to use ReadAtLeast from IO Package in Golang?
How to trim leading and trailing white spaces of a string in Golang?
How to play and pause execution of goroutine?
How to remove symbols from a string in Golang?