Go程序:演示map用法
在C++/Java中,map一般都以库的方式提供,比如在C++中是STL的std::map<>,在C#中是
Dictionary<>,在Java中是Hashmap<>,在这些语言中,如果要使用map,事先要引用相应的
库。而在Go中,使用map不需要引入任何库,并且用起来也更加方便。
/*
* 功能:演示map的用法 * 日期:2015年6月24日 */ package main import ( "fmt" ) func main () { // 创建student结构体 type student struct { id int name string sex string age int } // 创建map var student_db = make ( map [ int ] student ) student_db [ 1 ] = student { 101 , "李强" , "男" , 18 } student_db [ 2 ] = student { 102 , "钟云" , "男" , 20 } student_db [ 3 ] = student { 103 , "吴霞" , "女" , 19 } // 输出map fmt . Println ( student_db ) // 遍历map for i := 1 ; i <= len ( student_db ); i ++ { fmt . Println ( student_db [ i ]) } // 查询map id := 2 stu , found := student_db [ id ] if found { fmt . Println ( "The student with id " , id , " is found." , stu . name ) } else { fmt . Println ( "The student with id " , id , " is not found." ) } } 运行结果: map [ 3 :{ 103 吴霞 女 19 } 1 :{ 101 李强 男 18 } 2 :{ 102 钟云 男 20 }] { 101 李强 男 18 } { 102 钟云 男 20 } { 103 吴霞 女 19 } The student with id 2 is found . 钟云 |
Go程序:演示map用法
猜你喜欢
转载自blog.csdn.net/howard2005/article/details/79879244
今日推荐
周排行