On Tue, Dec 13, 2016 at 6:06 PM,  <sdic...@watchtower-security.com> wrote:
>
> So, I was talking with a friend about channels and mutexs, and the use of
> mutexs instead of channels. While I'm not sure why you'd want to, I was
> given this example and not an answer.
>
> In the example, he declares two channels. Input and Output. Then, he has a
> function running a for select loop in a gorutine. The input object accepts a
> text of body(string) and an ID (int), to which it stores it in a map. Then,
> the output channel accepts a request id (int), and a chan string. When
> something is put on the output channel, he gets the body of text out of the
> map and writes it to the output object's return channel and then closes it.
>
> My question, is this a good practice? Why would one want to use channels in
> place of mutexs? Is this efficient?

I expect that a mutex will be more efficient, and I would probably
write this code with a Mutex or a RWMutex.  But the advantage of this
code is that it is clearly free of concurrency problems and race
conditions.  Especially for more complex cases, that is not a
negligible feature.

Ian

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to