golang基础-map
我的理解map就是Python中的字典。
转载请注明出处:http://www.cnblogs.com/SSSR/p/6351816.html
参考链接:http://studygolang.com/articles/3637
map.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 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\n" , 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package learningmap import "fmt" import "testing" var print = fmt.Println func TestLearningMap(t *testing.T) { LearningMap() print( "这是一个测试!" ) } func TestLearningMap1(t *testing.T) { LearningMap1() print( "这是一个测试!" ) } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步