我在用Go语言进行编程。
假设有一个interface{}的变量,其中包含一个整数数组。我该如何将interface {}转换为[] int?
我试过 interface_variable,但出错了:
panic: interface conversion: interface is []interface {}, not []int
我在用Go语言进行编程。
假设有一个interface{}的变量,其中包含一个整数数组。我该如何将interface {}转换为[] int?
我试过 interface_variable,但出错了:
panic: interface conversion: interface is []interface {}, not []int
It's a []interface{}
not just one interface{}
, you have to loop through it and convert it:
http://play.golang.org/p/R441h4fVMw
func main() {
a := []interface{}{1, 2, 3, 4, 5}
b := make([]int, len(a))
for i := range a {
b[i] = a[i].(int)
}
fmt.Println(a, b)
}