Bootstrap

go: map

package main

import "fmt"

func main()  {
	
	// 声明方式

	// 只声明,不初始化,直接使用报错
	var myMap1 map[string] string
	// myMap1["one"] = "c"
	// 定义空间(初始化)
	myMap1 = make(map[string]string,10)
	myMap1["one"] = "c"
	myMap1["two"] = "c++"
	myMap1["three"] = "java"
	myMap1["four"] = "python"
	fmt.Println("mymap1 = " , myMap1)

	// 直接make
	mymap2 := make(map[string]string)
	mymap2["one"] = "c"
	mymap2["two"] = "c++"
	mymap2["three"] = "java"
	mymap2["four"] = "python"
	fmt.Println("mymap2 = " , mymap2)
	// 初始化时赋值
	mymap3 := map[string]string{
		"one":"c",
		"two":"c++",
		"three":"java",
		"four":"python",
	}
	fmt.Println("mymap3 = " , mymap3)


	// 遍历
	for key,value := range(mymap3){
		fmt.Print("key = ", key)
		fmt.Println(" , value = ", value)
	}

	// 删除
	delete(mymap3,"one")
	fmt.Println(mymap3) 
	
}

在这里插入图片描述

;