Go程序:演示map用法
【摘要】
Go程序:演示map用法
在C++/Java中,map一般都以库的方式提供,比如在C++中是STL的std::map<>,在C#中是
Dictionary<>,...
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 . 钟云 |
文章来源: howard2005.blog.csdn.net,作者:howard2005,版权归原作者所有,如需转载,请联系作者。
原文链接:howard2005.blog.csdn.net/article/details/79879244
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)