Go:创建包含映射数组的映射

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

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?

http://play.golang.org/p/UkokGzvtGL

答案1

得分: 7

  1. 没有方括号
  2. 每行末尾都要有逗号
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)
}
英文:
  1. No Square brackets
  2. 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)
}

huangapple
  • 本文由 发表于 2014年8月26日 10:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/25496944.html
匿名

发表评论

匿名网友

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

确定