## 一、map集合的聲明方式
~~~
// 聲明變量,默認 map 是 nil
var map_variable map[key_type]value_type
// 使用 make 函數
map_variable := make(map[key_type]value_type)
~~~
**示例:**
~~~
package main
import "fmt"
func main() {
var countries map[string]string
countries = make(map[string]string)
countries["France"] = "Paris"
countries["Italy"] = "羅馬"
countries["Japan"] = "東京"
countries["India"] = "新德里"
for country := range countries {
fmt.Println(country, "首都是", countries[country])
}
}
~~~
結果:
~~~
France 首都是 Paris
Italy 首都是 羅馬
Japan 首都是 東京
India 首都是 新德里
~~~
## 二、使用make生成map集合可以多傳一個容量參數,但由于map的特性(一旦容量不夠,會自動擴容),使得這個參數在使用的時候可忽略
```
package main
import "fmt"
func main() {
user := make(map[string]string, 3)
user["name"] = "tom"
user["gender"] = "male"
user["favorite"] = "basketball"
user["class"] = "one"
user["love"] = "own"
fmt.Print(user, len(user))
}
```
結果:map[name:tom gender:male favorite:basketball class:one love:own] 5
## 三、delete()刪除map集合中的元素
~~~
delete(countries, "France")
~~~