On 27/06/21, Brian Candler (b.cand...@pobox.com) wrote: > On Sunday, 27 June 2021 at 10:32:22 UTC+1 ro...@campbell-lange.net wrote: > > > By the way, as a golang newbie, it isn't clear to me when it is advisable > > to use a channel for locking as opposed to a mutex. Do people tend to use > > the former? > > > There is an absolutely excellent video on this topic by Rob Pike here: > https://www.youtube.com/watch?v=5zXAHh5tJqQ > > Once you've seen these patterns, you likely won't want to go back to > mutexes. > > For example: in your case, I'd create a one-element buffered channel > containing the data. Any function which wants to access that data can pop > the value off the channel, use and/or modify it, then push it back into the > channel when done. > > However if the *only* issue is around concurrent access to a single map, > then you could just use sync.Map <https://golang.org/pkg/sync/#Map>.
I considered sync.Map but since I have two maps I thought struct-level locking might make more sense. Thank you for the pointers and the excellent video (actually by Bryan C. Mills, it seems). errgroup.WithContext looks seriously helpful. While I was aware of the ability of a channel to act as a semphore the concepts of sharing through channels comes across very well, particularly sharing resources and data, concepts which open up a lot of possibilities. The video is eye-opening but will take a while to digest. Thanks again Rory -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/20210627183259.GA21570%40campbell-lange.net.