Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
20
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@suin(Craftsman Software)
Organization

Go言語: マップのキーが存在するかチェックしたい

Go言語でマップにキーが定義されているのかを調べる方法です。マップのキーを参照すると、値と存在有無のbool値が戻り値になります。このbool値を確認することで、キーの有無が分かります。

package main

import "fmt"

func main() {
    dict := map[string]int{
        "foo": 1,
    }

    if val, ok := dict["foo"]; ok {
        fmt.Printf("foo exists. The value is %#v", val)
    }
}

このコードを実行してみる

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
20
Help us understand the problem. What are the problem?