Given the scenario where you have a function which accepts t interface{}
. If it is determined that the t
is a slice, how do I range
over that slice? I will not know the incoming type, such as []string
, []int
or []MyType
, at compile time.
func main() {
data := []string{"one","two","three"}
test(data)
moredata := []int{1,2,3}
test(data)
}
func test(t interface{}) {
switch reflect.TypeOf(t).Kind() {
case reflect.Slice:
// how do I iterate here?
for _,value := range t {
fmt.Println(value)
}
}
}
Go Playground Example: http://play.golang.org/p/DNldAlNShB
Well I used reflect.ValueOf
and then if it is a slice you can call Len()
and Index()
on the value to get the len
of the slice and element at an index. I don't think you will be able to use the range operate to do this.
package main
import "fmt"
import "reflect"
func main() {
data := []string{"one","two","three"}
test(data)
moredata := []int{1,2,3}
test(moredata)
}
func test(t interface{}) {
switch reflect.TypeOf(t).Kind() {
case reflect.Slice:
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
fmt.Println(s.Index(i))
}
}
}
Go Playground Example: http://play.golang.org/p/gQhCTiwPAq
You don't need to use reflection if you know which types to expect. You can use a type switch, like this:
package main
import "fmt"
func main() {
loop([]string{"one", "two", "three"})
loop([]int{1, 2, 3})
}
func loop(t interface{}) {
switch t := t.(type) {
case []string:
for _, value := range t {
fmt.Println(value)
}
case []int:
for _, value := range t {
fmt.Println(value)
}
}
}
there is one exception from the way interface{} behaves, @Jeremy Wall gave already pointer. if the passed data is defined as []interface{} initially.
package main
import (
"fmt"
)
type interfaceSliceType []interface{}
var interfaceAsSlice interfaceSliceType
func main() {
loop(append(interfaceAsSlice, 1, 2, 3))
loop(append(interfaceAsSlice, "1", "2", "3"))
// or
loop([]interface{}{[]string{"1"}, []string{"2"}, []string{"3"}})
fmt.Println("------------------")
// and of course one such slice can hold any type
loop(interfaceSliceType{"string", 999, map[int]string{3: "three"}})
}
func loop(slice []interface{}) {
for _, elem := range slice {
switch elemTyped := elem.(type) {
case int:
fmt.Println("int:", elemTyped)
case string:
fmt.Println("string:", elemTyped)
case []string:
fmt.Println("[]string:", elemTyped)
case interface{}:
fmt.Println("map:", elemTyped)
}
}
}
output:
int: 1
int: 2
int: 3
string: 1
string: 2
string: 3
[]string: [1]
[]string: [2]
[]string: [3]
------------------
string: string
int: 999
map: map[3:three]
来源:https://stackoverflow.com/questions/14025833/range-over-interface-which-stores-a-slice