[英]Golang create a slice of maps
I was trying to create a slice of maps the following way.我试图通过以下方式创建一片地图。
keyvalue := make(map[string]interface{})
keyvalueslice := make([]keyvalue, 1, 1)
I was trying to create it just like the way string slice is created, however I am getting an error saying keyvalue is not a type.
我试图像创建字符串切片的方式一样创建它,但是我收到一条错误消息,说
keyvalue is not a type.
I am creating this slice to append data to keyvalueslice
variable later.我正在创建此切片以稍后将数据附加到
keyvalueslice
变量。
Can someone explain what is wrong?有人可以解释一下有什么问题吗?
keyvalue
is a variable not a type , you can't create a slice of variables. keyvalue
是一个变量而不是一个type ,你不能创建一个变量切片。 If you want to define custom type you can do this like如果你想定义自定义类型,你可以这样做
type keyvalue map[string]interface{}
then you can create a slice of keyvalue
s:然后你可以创建一个
keyvalue
的切片:
keyvalueslice := make([]keyvalue, 1, 1)
Example on playground操场上的例子
Or you can do this without defining custom type:或者您可以在不定义自定义类型的情况下执行此操作:
keyvalueslice := make([]map[string]interface{}, 1, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.