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 ov
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)
}
}
}
Check out the code on the playground.