在 go 中创建一片接口片
Creating a slice of slice of interfaces in go
我正在尝试创建一个函数,该函数 returns map
的所有 key, value
作为 slice
的 slice
元组(每个元组是 {key, value}
)
代码如下:
func ReturnTuples(map_ map[interface{}]interface{}) [][]interface{} {
toReturn := []([]interface{})
...
但是 toReturn
行出现错误:
type [][]interface {} is not an expression
我应该如何声明一个接口的切片?我认为这是唯一的方法。我试过没有括号,比如:
[][]interface{}
但也不行。
我试图在 google 上搜索 'golang slice of slice',但搜索结果很少。例如,我只找到了如何创建一个由 uint8
组成的简单的,即:[][]uint8
.
切片的元素类型是interface{}
,所以composite literal需要额外一对大括号:[]interface{}{}
.
如果是slice of slices:
toReturn := [][]interface{}{}
或者在使用 make()
时,您指定 类型 (而不是复合文字):
toReturn := make([][]interface{}, 0, len(map_))
您正在创建一个实例,而不是定义一个类型,因此您需要一对额外的大括号来初始化变量:
toReturn := [][]interface{}{}
我正在尝试创建一个函数,该函数 returns map
的所有 key, value
作为 slice
的 slice
元组(每个元组是 {key, value}
)
代码如下:
func ReturnTuples(map_ map[interface{}]interface{}) [][]interface{} {
toReturn := []([]interface{})
...
但是 toReturn
行出现错误:
type [][]interface {} is not an expression
我应该如何声明一个接口的切片?我认为这是唯一的方法。我试过没有括号,比如:
[][]interface{}
但也不行。
我试图在 google 上搜索 'golang slice of slice',但搜索结果很少。例如,我只找到了如何创建一个由 uint8
组成的简单的,即:[][]uint8
.
切片的元素类型是interface{}
,所以composite literal需要额外一对大括号:[]interface{}{}
.
如果是slice of slices:
toReturn := [][]interface{}{}
或者在使用 make()
时,您指定 类型 (而不是复合文字):
toReturn := make([][]interface{}, 0, len(map_))
您正在创建一个实例,而不是定义一个类型,因此您需要一对额外的大括号来初始化变量:
toReturn := [][]interface{}{}