我打印時的[]interface{}樣子是這樣的。[[1 2 3]]我想將其提取[1,2,3]為數組或切片,以便可以對其進行搜索。另一種解決方案是搜索類似1 in的元素[[1 2 3]],這也適用于我。
2 回答

萬千封印
TA貢獻1891條經驗 獲得超3個贊
你可以這樣做:
// if you know it is a first el in []interface{}
el := arr[0]
// and than you type cast it to []int
if arr, ok := el.([]int); ok {
fmt.Println(arr[0])
}
// or if you want to do the same thing for all elements (searching...)
for _, el := range arr {
if el, ok := el.([]int); ok {
// use el as a []int here
process(el)
}
}

汪汪一只貓
TA貢獻1898條經驗 獲得超8個贊
索引[]interface{}應該像interfaceSlice[0]
例子:
a := []interface{}{1, 2, 3, 4, 5}
d := []interface{}{a}
var b []int
e := d[0].([]interface{})
for i := range e {
b = append(b, e[i].(int))
}
fmt.Println(b)
- 2 回答
- 0 關注
- 251 瀏覽
添加回答
舉報
0/150
提交
取消