Let's say I have a function that returns an interface{}
. But I know that item returns is a slice of some kind. How can I determine the length of that slice? Here's sample code of what I tried, but they all cause compilation error.
package main
import (
"log"
"reflect"
)
func SomeKindOfSlice() interface{} {
return []int64{0,1,2,3,4,5,6,7,8,9}
}
func main() {
slice := SomeKindOfSlice()
/*log.Println(reflect.TypeOf(slice).Len())
log.Println(reflect.TypeOf(slice).Type().Len())
log.Println(reflect.ValueOf(slice).Type().Len())
log.Println(reflect.ValueOf(slice).Elem().Type().Len())
*/
log.Println(reflect.ValueOf(slice).Elem().Type().Len())
}
I'd like to avoid the brute force way of specifically type asserting the slice
variable just to find the length.
reflect.ValueOf(slice).Len()
– Judicator