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 5 years have passed since last update.

golang マップ

0
Last updated at Posted at 2019-09-26
package main

import (
	"fmt"
)

func main() {

	// 宣言で
	var soup map[string]int
	
	// 宣言しただけだとnill
	fmt.Println("宣言だけだとnillです:", soup == nil)

	
	// nillのマップの存在しないキーの参照
	measurement, status:= soup["onion"]
	
	// nillのマップの存在しないキーは、デフォ値 intは0
	fmt.Println("存在しないキー:" , measurement)
	
	// ステータスはfalse	
	fmt.Println("ステータス:", status)
	

	//makeで、代入
	m1 := make(map[string]int)
	m1["NHK"] = 1
	m1["日本テレビ"] = 4
	m1["テレビ朝日"] = 5
	m1["TBS"] = 6
	
	fmt.Println("makeのNHK:", m1["NHK"])


	//マップリテラルで代入
	m2 := map[string]int{
		"NHK": 1,
		"日本テレビ": 4,
		"テレビ朝日": 5,
		"TBS": 6,
	}
	
	fmt.Println("マップリテラルの日本テレビ:",m2["日本テレビ"])
	
}

0
0
1

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?