Quantcast
Channel: Why using `chan struct{}` when wait something done not `chan interface{}`? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Why using `chan struct{}` when wait something done not `chan interface{}`?

$
0
0

In golang, when we need to wait for something to finish, we will use a channel.

example:

done := make(chan struct{})go func() {    // ...    close(done)}()<-done

But, in other way, chan interface{} also works in this case.

So, what's the difference between chan struct{} and chan interface{}?

Example2:

done := make(chan struct{})go func() {    // ...    done <- struct{}{}}()<- done

In other case, if don't close channel in goroutine instead of send object to it.

There will create an object in goroutine, but if using chan interface{}, can be send nil object to channel.

Is it better to use chan struct{}?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>