36. Go - Interface{}
interface{} is a type that holds any type
package main
import (
"fmt"
)
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)
}
}
// "...interface{}" used here is used to make a table of any type given.
Example:
package main
import "fmt"
func main() {
folder := []interface{}{}
folder = append(folder, 5)
folder = append(folder, "string")
folder = append(folder, 124.14)
for _, e := range folder {
fmt.Println(e)
}
}
Comments
Post a Comment