「Go言語」Mapを使うサンプル
書式
var 変数名 map[string]string
使用例
package main import "fmt" func main() { var cityMap map[string]string cityMap = make(map[string]string) /*key value */ cityMap [ "Tokyo" ] = "東京" cityMap [ "Fukuoka" ] = "福岡" cityMap [ "Yokohama" ] = "横浜" /*mapの要素出力 */ for cft := range cityMap { fmt.Println(cft, " value is", cityMap [cft]) } /*element要素判定 */ kk, result := cityMap [ "key007" ] if (result) { fmt.Println("exit value ", kk) } else { fmt.Println(" not exist value") } }
実行結果
>go run test.go
Yokohama value is 横浜
Tokyo value is 東京
Fukuoka value is 福岡
not exist value