英文:
Go: create map with array of maps
问题
我正在尝试创建一个包含地图的数组的地图。我的代码:
Go:
func main() {
m := map[string][]map[string]string{
"photos": []map[string]string{{"a": "1"}, {"b": "2"}},
"pictures": []map[string]string{{"a": "1"}, {"b": "2"}},
}
fmt.Println(m)
}
这段代码有什么问题吗?这样做是可行的吗?
http://play.golang.org/p/UkokGzvtGL
英文:
I'm trying to create a map with an array containing maps. My code:
Go:
func main() {
m := map[string][]map[string]string{
"photos": [{"a":"1"}, {"b": "2"}],
"pictures": [{"a":"1"}, {"b": "2"}]
}
fmt.Println(m)
}
What is wrong with this? Is this possible?
答案1
得分: 7
- 没有方括号
- 每行末尾都要有逗号
package main
import "fmt"
func main() {
m := map[string][]map[string]string{
"photos": {{"a": "1"}, {"b": "2"}},
"pictures": {{"a": "1"}, {"b": "2"}},
}
fmt.Println(m)
}
英文:
- No Square brackets
- Always comma at the end of the line
http://play.golang.org/p/USm9kqDANn
package main
import "fmt"
func main() {
m := map[string][]map[string]string{
"photos": {{"a": "1"}, {"b": "2"}},
"pictures": {{"a": "1"}, {"b": "2"}},
}
fmt.Println(m)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论