LoginSignup
0
1

More than 1 year has passed since last update.

初学者がGo言語 map typesを勉強したよ

Posted at

mapとは何ぞや

Pythonでいう『辞書』と同じ、キーと値があるアレね。コードで書くと

dict = {"佐藤": 1, "加藤": 2, "武藤": 3}

それぞれ
"佐藤" "加藤" "武藤" = key
"1" "2" "3" = value
キーや値を取得したり追加、削除できるけど割愛します

Goでやってみる

環境:Go公式にあるPlaygroundを使用。環境構築要らず動作確認に便利です。(https://play.golang.org/)

package main

import (
    "fmt"
)

func main() {
    m := map[string]int{"すいか":100, "りんご":200}
    fmt.Println(m)              
    fmt.Println(m["すいか"])        //すいか の"value"を取得

    m["ぶどう"] = 300               //新しくkey,valueを追加
    fmt.Println(m)

    v, bool := m["りんご"]          //変数(bool)でtrue, falseを返す
    fmt.Println(v, bool)          //true判定 
    v1, bool1 := m["梨"]
    fmt.Println(v1, bool1)        //keyが存在しないので false
}


初歩の初歩だがここまで!
関連で make new スライス 配列等あるので勉強してから投稿します。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1