记一次坑爹的golang 二维map判断问题
现象
var PeopleLimit =make(map[string]map[string]int64)
func foo(arr1,arr2 string){
if x,ok := PeopleLimit[arr1][arr2];ok{
fmt.Println(" ok")
fmt.Println(x)
}else{
PeopleLimit[arr1] = make(map[string]int64)
fmt.Println(" not ok")
}
//添加新元素
PeopleLimit[arr1][arr2] = time.Now().Unix()
}
func main() {
foo("a","b")
foo("c","d")
foo("a","f")
fmt.Println(PeopleLimit)
return
}
按理应该要输出三个元素,但第一个被覆盖了,定位了很久才发现是二维map的判断问题:
执行foo("a","f")时if x,ok := PeopleLimit[arr1][arr2];判断是二级key也不存在,但在执行第三次foo("a","f")时,map[a][b]是存在,但map[a][f]不存在,于是,进入else后重新make将map[a]清空了!!
修改后:
var PeopleLimit =make(map[string]map[string]int64)
func foo(arr1,arr2 string){
if x,ok := PeopleLimit[arr1];ok{
//修改点
if y,ok := x[arr2];ok{
fmt.Println(" ok")
fmt.Println(x,y)
}
}else{
PeopleLimit[arr1] = make(map[string]int64)
fmt.Println(" not ok")
}
//添加新元素
PeopleLimit[arr1][arr2] = time.Now().Unix()
}
func main() {
foo("a","b")
foo("c","d")
foo("a","f")
fmt.Println(PeopleLimit)
return
}
---------------------
作者:yinnnnnnn
来源:CSDN
原文:https://blog.csdn.net/qq_35440678/article/details/83154780
版权声明:本文为博主原创文章,转载请附上博文链接!
记一次坑爹的golang 二维map判断问题的更多相关文章
- Codeforces 519D A and B and Interesting Substrings(二维map+前缀和)
题目链接:http://codeforces.com/problemset/problem/519/D 题目大意:给你一串字符串s仅由小写字母组成,并且对于'a'~'z'都给了一个值.求子串t满足t的 ...
- HDU 1263 二维map
题意:给出一份水果的交易表,根据地区统计出水果的交易情况. 思路:二维map使用. #include<cstdio> #include<string> #include ...
- ACM-ICPC2018徐州网络赛 Features Track(二维map+01滚动)
Features Track 31.32% 1000ms 262144K Morgana is learning computer vision, and he likes cats, too. ...
- Golang 二维切片初始化
package main import "fmt" func main() { // 方法0 row, column := 3, 4 var answer [][]int for ...
- golang 二维切片
初始化: res := make([][length]int, length), 例如: res := make([][2]int, 10) fmt.Println(res) 输出: [[0 0] [ ...
- golang二维码
package main import ( "github.com/boombuler/barcode" "github.com/boombuler/barcode/qr ...
- 扫描二维码判断移动设备(Android/ios),以及判断是否微信端扫描
<section class="download"> <a href="apk地址" class="android" st ...
- hdu 1263 水果 【二维map】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263 题目大意: Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~ ...
- Golang 中使用多维 map
http://tnt.wicast.tk/2015/11/02/golang-multiple-dimension-map/ Golang 的 XML/JSON 解析库乍看使用起来很方便,只要构造一样 ...
随机推荐
- PLSQL_Oracle基本概念总结(汇总)
2014-08-16 Created By BaoXinjian
- sublime text怎样安装ctags来定位函数
sublime确实是一款非常不错的开发软件.用起来非常爽,里面集成了非常多插件.仅仅要安装就可以, 下来来介绍下sublime中ctags插件的安装,安装这个插件之后就能够高速定位某函数了,很方便. ...
- hibernate,动态更新,插入 dynamic-insert,dynamic-update 我有话要说 ---稍后整理
http://dreamzhong.iteye.com/blog/1207377 http://blog.csdn.net/hsuxu/article/details/8108326 @org.hib ...
- 枚举操作的常用方法,包括获得枚举的value,name,description
public enum SendInfoStateEnum { [Description("等待提交")] 等待提交 = 1, [Description(& ...
- linux-CentOS6.4安装Memcached+memcached扩展+安装memcache扩展+Memcache+mecached同步SESSION的几种方法
一.编译环境的准备 yum install gcc yum install gcc-c++ libstdc++-devel yum install zlib-devel 二.源代码包准备 wget ...
- css3导航hover悬停效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- asp.net kindeditor 后台取不到数据
今晚搞了3个小时,才搞定这个破问题. 页面上使用kindeditor,提交的时候使用了LinkButton,按钮后台事件私活娶不到kineditor里的数据. 最终一步一步测试,终于发现是LinkBu ...
- 每日英语:Cyclists Live Six Years Longer
Cycling does the body good. New data from Tour de France cyclists finds that those athletes live an ...
- 每日英语:Does Evolution Want Us To Be Unhappy?
Samuel Johnson called it the vanity of human wishes, and Buddhists talk about the endless cycle of d ...
- 【转】 OpenGL使用libPng读取png图片
觉得自己越来越无耻了呢?原文:http://laoyin.blog.51cto.com/4885213/895554 我复制到windows下也可以正常跑出来. #include<stdarg. ...