为了调试一些并发问题,我正在将部分代码从处理常规Golang映射切换为处理sync.Map。但是,当我尝试运行新代码时,遇到两个不确定的调试错误。
原始代码块:
sync_mutex.Lock()
if _,ok := the_map[cur_h]; ok {
the_map[cur_h] = append(the_map[cur_h],cur_id)
} else {
value := []int{cur_id}
the_map[cur_h] = value
}
sync_mutex.Unlock()
新代码块:
if _,ok := sync_map.Load(cur_h); ok {
sync_mutex.Lock()
cur_slice,_ := sync_map.Load(cur_h)
updtd_slice := append(cur_slice,cur_id) // error #1
sync_map.Store(cur_h,updtd_slice)
map_mutex.Unlock()
} else {
map_mutex.Lock()
value := []int{cur_id}
sync_map.Store(cur_h,[]int{value}) // error #2
sync_mutex.Unlock()
}
所引发的错误是:
... first argument to append must be slice; have interface {}
... cannot use value (type []int) as type int in array or slice literal