• go 集合


    1.定义一个集合 感觉和python的字典差不多

    func main() {
    	//定义一个集合
    	var countryCapitalMap map[string]string /*创建集合 */
    	//创建集合
    	countryCapitalMap = make(map[string]string)
    
    	/* map插入key - value对,各个国家对应的首都 */
    	countryCapitalMap["France"] = "Pais"
    	countryCapitalMap["Italy"] = "罗马"
    	countryCapitalMap["Japan"] = "东京"
    	countryCapitalMap["India "] = "新德里"
    	fmt.Println(countryCapitalMap)
    
    }
    

      

    2.循环输出key value

    func main() {
        var countryCapitalMap map[string]string /*创建集合 */
        countryCapitalMap = make(map[string]string)
    
        /* map插入key - value对,各个国家对应的首都 */
        countryCapitalMap["France"] = "Paris"
        countryCapitalMap["Italy"] = "罗马"
        countryCapitalMap["Japan"] = "东京"
        countryCapitalMap["India "] = "新德里"
    
        for country := range countryCapitalMap {
            fmt.Println(country, countryCapitalMap[country])
    
        }
    }
    

      

    3.判断key是否存在

    var countryCapitalMap map[string]string /*创建集合 */
    	countryCapitalMap = make(map[string]string)
    
    	/* map插入key - value对,各个国家对应的首都 */
    	countryCapitalMap["France"] = "Paris"
    	countryCapitalMap["Italy"] = "罗马"
    	countryCapitalMap["Japan"] = "东京"
    	countryCapitalMap["India "] = "新德里"
            //ok为bool值,为true那么captial就是这个key的value
    	captial, ok := countryCapitalMap["Italy"]
    	if ok {
    		fmt.Println(ok, "存在首都是", captial)
    
    	} else {
    
    		fmt.Println("不存在")
    
    	}
    

      

    4.删除key value

       var countryCapitalMap map[string]string /*创建集合 */
        countryCapitalMap = make(map[string]string)
    
        /* map插入key - value对,各个国家对应的首都 */
        countryCapitalMap["France"] = "Paris"
        countryCapitalMap["Italy"] = "罗马"
        countryCapitalMap["Japan"] = "东京"
        countryCapitalMap["India "] = "新德里"
        delete(countryCapitalMap, "France")
        fmt.Println(countryCapitalMap)
    

      

  • 相关阅读:
    P2519 [HAOI2011]problem a
    P1084 疫情控制
    P1941 飞扬的小鸟
    NOIP填坑计划
    P2831 愤怒的小鸟
    AGC 16 D
    P3960 列队
    Python3爬虫相关软件,库的安装
    软件理论基础—— 第一章命题逻辑系统L
    软件理论基础——导论
  • 原文地址:https://www.cnblogs.com/Jack-cx/p/10182650.html
Copyright © 2020-2023  润新知