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 check lowercase characters in a string in Golang?
What is GOPATH?
Different ways to validate JSON string
Creating a Function in Golang
How to declare empty Map in Go?
How to Convert string to integer type in Go?
How do you write multi-line strings in Go?
How to create a photo gallery in Go?
How to get struct variable information using reflect package?
Replace first occurrence of string using Regexp