0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【備忘録】GolangでMapを利用する。

Last updated at Posted at 2022-05-17

GolangでMapを利用する。

main.go
package main

import "fmt"

func main() {
	// ユーザIDと名前のMapを作成する。
	userIDToNameMap := map[int]string{123: "tanaka", 234:"takeru", 345: "kkfactory"}
	fmt.Println("userIDToNameMap: ",userIDToNameMap)

	// Keyに対するValueを取得する。
	fmt.Println("userIDが123の値は", userIDToNameMap[123])

	// 要素を追加する。
	userIDToNameMap[456]="takeshi"
	fmt.Println("userIDToNameMap: ",userIDToNameMap)

	// Valueの値を更新する
	userIDToNameMap[123]="kouji"
	fmt.Println("userIDが123の値は", userIDToNameMap[123])

	// 番号が含まれているか確認する。
	_, ok := userIDToNameMap[123]
    fmt.Println("userIDに123が含まれているか?",ok) // 
}
実行結果
~/go/src/array_slice $ go run main.go
userIDToNameMap:  map[123:tanaka 234:takeru 345:kkfactory]
userIDが123の値は tanaka
userIDToNameMap:  map[123:tanaka 234:takeru 345:kkfactory 456:takeshi]
userIDが123の値は kouji
userIDに123が含まれているか? true

GolangでMapをネストし利用する。

main.go
import "fmt"

func main() {
	classAndMemberMap := map[string]map[int]string{
		"A組":map[int]string{},
	}
	// A組の中に追加する。
	classAndMemberMap["A組"][123] = "tanaka"
	classAndMemberMap["A組"][234] = "takeru"
	classAndMemberMap["A組"][345] = "kkfactory"

	fmt.Println("classAndMemberMap: ", classAndMemberMap)
}
実行結果
~/go/src/array_slice $ go run main.go
classAndMemberMap:  map[A組:map[123:tanaka 234:takeru 345:kkfactory]]

GolangでMapにStructを利用する

main.go
package main

import "fmt"

type ClassMember struct {
	UserID      int
	Name        string
}

func main() {
	classToMemberMap := make(map[string]*ClassMember)
	classToMemberMap["A組"] = &ClassMember{
		UserID: 123,
		Name:   "tanaka",
	}
	classToMemberMap["B組"] = &ClassMember{
		UserID: 234,
		Name:   "kouji",
	}
	fmt.Println("result: ", classToMemberMap)
	fmt.Println("result_A組: ", *classToMemberMap["A組"])
	fmt.Println("result_B組: ", *classToMemberMap["B組"])
}
実行結果
~/go/src/array_slice $ go run main.go
result:  map[A組:0xc0000981b0 B組:0xc0000981e0]
result_A組:  {123 tanaka }
result_B組:  {234 kouji }

GolangでMapにStructを利用する(配列変)

main.go
package main

import "fmt"

type ClassMember struct {
	UserID int
	Name   string
}

func main() {
	classToMemberMap := make(map[string][]ClassMember)

	class1 := []ClassMember{
		{
			UserID: 123,
			Name:   "tanaka",
		},
	}

	class2 := []ClassMember{
		{
			UserID: 234,
			Name:   "kouji",
		},
	}

	classToMemberMap["A組"] = class1
	classToMemberMap["B組"] = class2

	fmt.Println("classToMemberMap: ", classToMemberMap)

}
実行結果
~/go/src/array_slice $ go run main.go
classToMemberMap:  map[A組:[{123 tanaka}] B組:[{234 kouji}]]
main.go
package main

import "fmt"

type ClassMember struct {
	UserID int
	Name   string
}

func main() {
	classToMemberMap := make(map[string][]ClassMember)

	class1 := []ClassMember{
		{
			UserID: 123,
			Name:   "tanaka",
		},
	}
	class2 := []ClassMember{
		{
			UserID: 234,
			Name:   "kouji",
		},
	}

	classToMemberMap["A組"] = class1
	classToMemberMap["B組"] = class2

	fmt.Println("classToMemberMap[A組]", classToMemberMap["A組"])

	classMember1 := ClassMember{
		UserID: 345,
		Name: "kkfactory",
	}

	classToMemberMap["A組"] = append(classToMemberMap["A組"], classMember1)

	fmt.Println("classToMemberMap: ", classToMemberMap)

}
実行結果
~/go/src/array_slice $ go run main.go
classToMemberMap[A組] [{123 tanaka}]
classToMemberMap:  map[A組:[{123 tanaka} {345 kkfactory}] B組:[{234 kouji}]]
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?