Go []接口{}的元素类型

Go []接口{}的元素类型,go,reflect,Go,Reflect,如何获取[]接口{}的运行时元素类型 我尝试了以下测试 var data interface{} temp := make([]interface{}, 0) temp = append(temp, int64(1)) data = temp elemType := reflect.TypeOf(data).Elem() switch elemType { case reflect.TypeOf(int64(1)): logger.Infof("type: int64 ") defau

如何获取
[]接口{}
的运行时元素类型

我尝试了以下测试

var data interface{}
temp := make([]interface{}, 0)
temp = append(temp, int64(1))
data = temp

elemType := reflect.TypeOf(data).Elem()
switch elemType {
case reflect.TypeOf(int64(1)):
    logger.Infof("type: int64 ")
default:
    logger.Infof("default %v", elemType.Kind()) // "default" is matched in fact

}

[]接口{}
的元素类型是
接口{}

如果您想在该切片中获得单个值的动态类型,则需要索引到该切片中以提取这些值

data := make([]interface{}, 0)
data = append(data, int64(1))
data = append(data, "2")
data = append(data, false)

typeof0 := reflect.ValueOf(data).Index(0).Elem().Type()
typeof1 := reflect.ValueOf(data).Index(1).Elem().Type()
typeof2 := reflect.ValueOf(data).Index(2).Elem().Type()

那么,你的问题是什么?所以你想问你的期望输出是
类型:int64
,但是你得到的是
默认值…
?@KamolHasan是的,我认为元素类型是真正的数据运行时类型,而不是接口。