创建一个包含映射的切片。

huangapple go评论82阅读模式
英文:

Creating a Slice that contains a map

问题

我正在尝试创建一个包含切片和映射的数据结构。这是我的代码:

data := map[string]interface{}{"Offset": "0", "Properties": []string{}, "Category": "all", "Locations": []string{}, "Accounts": "100"}

我需要"Properties"元素包含一个像这样的映射:

{"key": "Type", "value": "User"}

这似乎比应该的要难,或者可能是我太蠢了。

英文:

I'm trying to create a data structure that includes a slice containing a map. This is what I have:

data := map[string]interface{}{"Offset": "0", "Properties": []string{}, "Category": "all", "Locations": []string{}, "Accounts": "100" } 

I need the "properties" element to include a map that looks like this:

{"key": "Type", "value": "User"}

This seems to be harder than it should be, or maybe I'm just being stupid.

答案1

得分: 2

以下是您要翻译的内容:

像这样的代码应该是您想要的(playground

data := map[string]interface{}{"Offset": "0",
    "Properties": map[string]string{
        "key":   "Type",
        "value": "User",
    },
    "Category":  "all",
    "Locations": []string{},
    "Accounts":  "100",
}
英文:

Something like this should be what you want (playground)

data := map[string]interface{}{"Offset": "0",
	"Properties": map[string]string{
		"key":   "Type",
		"value": "User",
	},
	"Category":  "all",
	"Locations": []string{},
	"Accounts":  "100",
}

huangapple
  • 本文由 发表于 2014年12月18日 06:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/27535685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定