close_breaker.mx raw

   1  // A channel that is only closed and received from is a breaker, not an
   2  // unfinished pair: close is the broadcast sender. Moxie's spawn model is built
   3  // on exactly this - a chan struct{} breaker is closed to wake every listener,
   4  // and no value is ever sent.
   5  //
   6  // The legacy checker counted only ssa.Send as a sender, so a close-only channel
   7  // with a receive reported "channel has no sender". Closing a channel with no
   8  // send is legal and its listeners wake with the zero value, so the checker now
   9  // counts a close of the channel as its sender too.
  10  package main
  11  
  12  import "fmt"
  13  
  14  func main() {
  15  	// Termination: a batch program must still be stoppable, so the entry
  16  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  17  	select {
  18  	case <-shutdown:
  19  		return
  20  	default:
  21  	}
  22  
  23  	done := chan struct{}{}
  24  	close(done)
  25  	<-done // a closed channel receives immediately
  26  	fmt.Println("close breaker ok")
  27  }
  28