golang 学习(十五)map的定义和基本用法

map的定义和基本用法

声明

	//1.make 定义
	var userData = make(map[string]string)
	userData["name"] = "wang"
	fmt.Println(userData["name"])
	fmt.Println(userData)
   //2.
	var userData = map[string]string{
     "name": "li", "age": "20"}
	fmt.Println(userData["age"]) //20
	//3.
	userData := map[string]string{
     "name": "li", "age": "20", "sex": "男"}
	fmt.Println(userData["age"]) //20

查找map

	userData := map[string]string{
     "name": "li", "age": "20", "sex": "男"}
    v, ok := userData["age"]
	fmt.Println(v, ok)        // 20 true
	val, status := userData["age1"] //age1不存在
	fmt.Println(val, status)        // 空 false

删除map中的键值对 delete()

	userData := map[string]string{
     "name": "li", "age": "20", "sex": "男"}
	fmt.Println(userData) //map[age:20 name:li sex:男]
	delete(userData,"sex")
	fmt.Println(userData) //map[age:20 name:li]

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