On Thu, 30 Jul 2015 3:50 AM Roberto Zanotto <roby...@gmail.com> wrote:
I guess the buffer is to make communication asynchronous. With unbuffered channels, when a goroutine sends a message to the channel, it blocks until there's another goroutine that is ready to receive the message (the channel does both communication _and_ synchronization between goroutines). If you use a buffer instead, the message is dropped in the buffer and the send operation is not blocking (if there's room in the buffer, of course).
That said, writing to a buffered channel is probably not the best way of signaling completion. It's best to close the channel instead of writing to it (and possibly using a chan of struct{}). Closing operation is always non blocking and if other goroutines (also more than one) are waiting to read a message they will get the zero value of the message when the channel gets closed.
But what if a zero value is a valid channel value to process? A done channel would mean done.
Hope I was able to explain it clearly :) maybe you should watch a video about concurrency patterns in Go.
Cheers.
On Wednesday, July 29, 2015 at 4:14:21 PM UTC+2, Yesudeep Mangalapilly wrote:
Namaste,
I'm new to concurrency and Go, so my question may be a bit naive.
While reading through this example here:
http://play.golang.org/p/0DfW-1RMqi
--- code ---
package main
import "fmt"
import "time"
func worker(done chan bool) {
time.Sleep(time.Second)
done <- true
}
func main() {
done := make(chan bool, 1) // What is the significance of using 1 as capacity here?
go worker(done)
<-done
}
--- end code ---
Why use 1 to create a buffered channel? Can we not signal "done" using a regular non-buffered
channel?
Cheers,
Yesudeep.
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<-done