On Friday, 27 November 2020 at 06:14:48 UTC Afriyie Abraham Kwabena wrote:

> What I would like to ask is, using mutex OK and if not the best way of 
> solving it, how can i use
> channels in this case.
>

There's nothing wrong with mutex, but you can use channels for a more 
native-Go experience.
This video is well worth 
watching: https://www.youtube.com/watch?v=5zXAHh5tJqQ

In short, you can get mutex or semaphore-like behaviour by having a channel 
with fixed depth, and putting/pulling values from it.
Playground <https://play.golang.org/p/Vg-c8v7N0-6>

mutex := make(chan struct{}, 1)
...

mutex <- struct{}{}
... do stuff
<-mutex

-- 
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/a3c1e84d-5d68-4862-a94a-9180ae943485n%40googlegroups.com.

Reply via email to