1. 使用make分配map内存

m := make(map[string]int)

2.给map赋值

m["a"] = 0
	m["b"] = 1
	m["c"] = 2
	m["d"] = 3
	m["e"] = 4
	m["f"] = 5
	m["g"] = 6
	m["h"] = 7

3.遍历map

fmt.Println("map:", m, "map长度:", len(m))
	//遍历map,无序输出
	for k, v := range m {
		fmt.Println("键:", k, "值:", v)
	}

4.对map进行删除,清空,长度计算操作

fmt.Println("取map中指定键f的值:", m["f"])
	v, ret := m["f"]
	fmt.Println("值:", v, "结果:", ret)
	delete(m, "f")
	fmt.Println("删除map中键为f的元素后:", m, "map长度:", len(m))
	clear(m)
	fmt.Println("调用clean函数清空map后:", m, "map长度:", len(m))

5.map对象比较

//直接初始化map
	m1 := map[string]int{"a1": 1, "a2": 2}
	fmt.Println(m1["a1"], m1["a2"], m1, len(m1))
	m2 := map[string]int{"a1": 1, "a2": 2}
	//map对象比较
	tmp := ""
	if maps.Equal(m1, m2) {
		tmp = "YES"
	} else {
		tmp = "NO"
	}
	fmt.Println("map m1与m2是相等的:", tmp)
	//遍历map
	for k, v := range m1 {
		fmt.Println("键:", k, "值:", v)
	}

6.数组初始化与遍历

//数组初始化
	nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
	//数组遍历
	for k, v := range nums {
		fmt.Println("of nums: ===> ", "key:", k, "value:", v)
	}

7.map与数组交互使用

stringMap := make(map[string]string)
	stringArr := []string{"中国", "美国", "日本"}
	stringArr1 := []string{"ZH", "US", "JP"}
	for i, v := range stringArr1 {
		stringMap[v] = stringArr[i]
	}
	for k, v := range stringMap {
		fmt.Println(k, v)
	}

完整示例:

//maps使用
	m := make(map[string]int)
	m["a"] = 0
	m["b"] = 1
	m["c"] = 2
	m["d"] = 3
	m["e"] = 4
	m["f"] = 5
	m["g"] = 6
	m["h"] = 7
	fmt.Println("map:", m, "map长度:", len(m))
	//遍历map,无序输出
	for k, v := range m {
		fmt.Println("键:", k, "值:", v)
	}
	fmt.Println("取map中指定键f的值:", m["f"])
	v, ret := m["f"]
	fmt.Println("值:", v, "结果:", ret)
	delete(m, "f")
	fmt.Println("删除map中键为f的元素后:", m, "map长度:", len(m))
	clear(m)
	fmt.Println("调用clean函数清空map后:", m, "map长度:", len(m))
	//直接初始化map
	m1 := map[string]int{"a1": 1, "a2": 2}
	fmt.Println(m1["a1"], m1["a2"], m1, len(m1))
	m2 := map[string]int{"a1": 1, "a2": 2}
	//map对象比较
	tmp := ""
	if maps.Equal(m1, m2) {
		tmp = "YES"
	} else {
		tmp = "NO"
	}
	fmt.Println("map m1与m2是相等的:", tmp)
	//遍历map
	for k, v := range m1 {
		fmt.Println("键:", k, "值:", v)
	}

	//数组初始化
	nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
	//数组遍历
	for k, v := range nums {
		fmt.Println("of nums: ===> ", "key:", k, "value:", v)
	}

	stringMap := make(map[string]string)
	stringArr := []string{"中国", "美国", "日本"}
	stringArr1 := []string{"ZH", "US", "JP"}
	for i, v := range stringArr1 {
		stringMap[v] = stringArr[i]
	}
	for k, v := range stringMap {
		fmt.Println(k, v)
	}