IMHO, this is so that the sender of ths signal to your channel doesn't gets 
blocked. 

A helpful blog to understand 
it: https://www.rudderstack.com/blog/implementing-graceful-shutdown-in-go/

Hope it helps!

On Thursday 30 July 2015 at 02:42:50 UTC+5:30 Bakul Shah wrote:

> On Wed, 29 Jul 2015 01:39:50 PDT Yesudeep Mangalapilly <yesu...@gmail.com> 
> wrote:
> > --- 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?
>
> In general, synchronize only when you have to. Here the main
> thread wants to know when the worker thread terminates but the
> worker thread doesn't care when the main thread gets around to
> reading from "done". Using a 1 deep buffer channel exactly
> captures this usage pattern. An unbuffered channel would make
> the worker thread "rendezvous" with the main thread, which is
> unnecessary.
>

-- 
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/6ef0468a-fd7d-4bf8-833b-a7696512aa4dn%40googlegroups.com.

Reply via email to