[Go] Map mapping

package main

import "fmt"

func main() {
    
    

	Map := make(map[string] int)

	Map["张全蛋"] = 23
	Map["ReganYue"] = 20

	fmt.Println(Map["ReganYue"])

	age,ok := Map["aaa"]
	fmt.Println(age,ok)
	//if ok == true 可以简写为 if ok
	if ok {
    
    
		fmt.Println("年龄是%d",age)
	}else {
    
    
		fmt.Println("查你妹,没有这个卵人")
	}

	if age,ok := Map["nil"];ok==true{
    
    
		fmt.Println("年龄是%d",age)
	}else{
    
    
		fmt.Println("查你妹,没有这个卵人")
	}
	//两种遍历方式
	for key,value := range Map{
    
    
		fmt.Printf("%d,%s\n",value,key)
	}
	for key := range Map{
    
    
		fmt.Printf("%d,%s\n",Map[key],key)
	}
}

Guess you like

Origin blog.csdn.net/qq_36045898/article/details/113823237