Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

go - Working with Slices and Golang sync.Map Structure

In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug.

The original code block:

        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()

The new code block:

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()

            }

The error that is being thrown is:

... first argument to append must be slice; have interface {}
... cannot use value (type []int) as type int in array or slice literal
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...