golang学习笔记之map元素删除

golang学习笔记之map元素删除

golang map元素删除要比c++方便多了,可以直接在循环遍历的时候删除元素,而不用担心程序崩溃;

示例代码如下:

package main

import (
	"fmt"
)

func main() {
	infoMap := make(map[int]string)
	infoMap[1] = "1"
	infoMap[2] = "2"
	infoMap[3] = "3"
	infoMap[4] = "4"
	infoMap[5] = "5"
	for k, _ := range infoMap {
		if 3 == k {
			delete(infoMap, k)
		}
	}

	fmt.Println(infoMap)
}

输出结果如下:

map[4:4 5:5 1:1 2:2]

你可能感兴趣的:(golang,go,golang,go,map)