package mainimport ( "fmt")func main() { var x = map[string]string{ "a": "aaa", "b": "bbb", } for key, value := range x { fmt.Println("key:", key, "value:", value) }}
如果先声明,后初始化:
package mainimport ( "fmt")func main() { var x map[string]string x = make(map[string]string) //make是必须的 x["a"] = "aaa" x["b"] = "bbb" for key, value := range x { fmt.Println("key:", key, "value:", value) }}