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
0
Help us understand the problem. What are the problem?

【Golang】mapに複数のキーを設定する

個人的に感動的に便利だったので、書いておきます。

コード

mapのキーにstructを設定すると出来ます。
複数のキーで一つの値を持っておきたい時に便利です

package main

import (
    "fmt"
)

type key struct {
    key1 string
    key2 string
}

func main() {
    m := map[key]string{
        key{
            key1: "hoge_key1",
            key2: "hoge_key2",
        }: "hoge",
    }

    fmt.Println(m)
}
// 結果:map[{hoge_key1 hoge_key2}:hoge]
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
0
Help us understand the problem. What are the problem?