Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...