zoukankan      html  css  js  c++  java
  • golang基础-map

    我的理解map就是Python中的字典。

    转载请注明出处:http://www.cnblogs.com/SSSR/p/6351816.html

    参考链接:http://studygolang.com/articles/3637

    map.go

    package learningmap
    
    import "fmt"
    
    type personInfo struct {
    	ID      string
    	Name    string
    	Address string
    }
    
    func LearningMap() {
    	
    	//var numbers map[string]int
    	var myMap map[string]personInfo
    	
    	rating := map[string]float32{"C": 5, "Go": 4.5, "Python": 4.5, "C++": 2}
    	myMap = map[string]personInfo{"1234": personInfo{"1", "Jack", "Room 101,..."}}
    	fmt.Println(rating["C"])
    	fmt.Println(myMap["1234"])
    	fmt.Println("ID:", myMap["1234"].ID)
    	for key, value := range rating {
    		fmt.Println("Key:", key, "Value:", value)
    	}
    }
    
    func LearningMap1() {
    	type personInfo struct {
    	ID      string
    	Name    string
    	Address string
    }
    	 /*
        //声明一个map变量numbers,键名为string,值为int
        var numbers map[string] int
        //给map变量创建值,同时指定最多可以存储3个int值
        numbers = make(map[string] int, 3)
        //map元素赋值
        numbers["one"] = 1
        numbers["two"] = 2
        numbers["three"] = 3
        */
    
        //上面方式的简写方法
        numbers := map[string] int{
            "one": 1,
            "two": 2,
            "three": 3,
        }
        
        var myMap map[string] personInfo
        myMap = make(map[string] personInfo)
        myMap["persion1"] = personInfo{"1", "Amiee", "Street 101"}
        myMap["persion2"] = personInfo{"2", "Beva", "Street 102"}
        myMap["persion3"] = personInfo{"3", "Cencey", "Street 103"}
        
        /*
        // 上面方式的简写方法
        myMap := map[string] persionInfo{
            "persion1": personInfo{"1", "Amiee", "Street 101"},
            "persion2": personInfo{"2", "Beva", "Street 102"},
            "persion3": personInfo{"3", "Cencey", "Street 103"},
        }
        */
        
        //map元素打印
        fmt.Printf("%v
    ", numbers)
        fmt.Println(numbers)
        fmt.Println(numbers["two"])
    
        fmt.Println(myMap)
        fmt.Println(myMap["persion1"])
        
        //map元素查找
        p1, ok := myMap["persion1"]
        if ok{
            fmt.Println("Found persion1, name", p1.Name, ", info", p1 )
        }else{
            fmt.Println("Not Found persion1")
        }
        
        //map元素修改
        //map是一种引用类型,如果两个map同时指向一个底层,那么一个改变,另一个也相应的改变。
        numbersTest := numbers
        numbersTest["one"] = 11
        fmt.Println(numbers)   
    
        //map元素删除
        delete(numbers, "one")
        fmt.Println(numbers)    
    }
    
    /*
       输出结果如下
    
    
    [root@localhost mygo]# go run  test.go 
    map[one:1 two:2 three:3]
    map[one:1 two:2 three:3]
    2
    map[persion1:{1 Amiee Street 101} persion2:{2 Beva Street 102} persion3:{3 Cencey Street 103}]
    {1 Amiee Street 101}
    Found persion1, name Amiee , info {1 Amiee Street 101}
    map[one:11 two:2 three:3]
    map[two:2 three:3]
    }
    */
    

      map_test.go

    package learningmap
    
    import "fmt"
    import "testing"
    
    var print = fmt.Println
    
    func TestLearningMap(t *testing.T) {
    	LearningMap()
    	print("这是一个测试!")
    }
    
    
    func TestLearningMap1(t *testing.T) {
    	LearningMap1()
    	print("这是一个测试!")
    }
    

      

  • 相关阅读:
    11月7日
    11月6日
    11月5日
    11月4日
    html5
    intern
    pjtool用到的数据库----oracle范畴
    运用的jdk版本如何查看
    JAVA 一句话技巧
    [ios 开发笔记]:一句话笔记
  • 原文地址:https://www.cnblogs.com/SSSR/p/6351816.html
Copyright © 2011-2022 走看看