マップ
連想配列に類するデータ構造。
任意のキーと、任意の値を保持することが出来る。
Code
func main() {
// マップの生成
abcMap := make(map[int]string)
fmt.Println(abcMap)
// 値の代入
abcMap[1] = "A"
abcMap[2] = "B"
abcMap[3] = "C"
fmt.Println(abcMap)
// リテラルを用いた生成
abcdeMap := map[int]string{1:"A", 2:"B", 3:"C", 4:"D", 5:"E"}
fmt.Println(abcdeMap)
// 要素の参照
fmt.Println(abcdeMap[1])
fmt.Println(abcdeMap[5])
// 要素数
fmt.Println(len(abcMap))
// 要素の削除
delete(abcMap, 2)
fmt.Println(abcMap)
// forによる出力
for i, m := range abcdeMap {
fmt.Println(strconv.Itoa(i) + " : " + m)
}
}
Output Sample
~ $ go build map.go
~ $ ./map
map[]
map[1:A 2:B 3:C]
map[1:A 2:B 3:C 4:D 5:E]
A
E
3
map[1:A 3:C]
1 : A
2 : B
3 : C
4 : D
5 : E
GitHub