Thank you. Would you have an explanation for why it works so differently from using default?
On Wednesday, 24 October 2018 22:44:26 UTC+5:30, Burak Serdar wrote: > > You can do this instead: > > https://play.golang.org/p/avMIyYlwxbF > On Wed, Oct 24, 2018 at 11:05 AM Sathish VJ <sath...@gmail.com > <javascript:>> wrote: > > > > This is a program where the receiver is asking the sender to close after > receiving 1 value. Using default for the sending (line 34) causes the code > to deadlock while even a timeout of a nanosecond causes it to be ok. > > I don't want to time the sending though. Is there a better option to > sequence this code? > > Is using default not an option here at all? > > > > https://play.golang.org/p/30sI9RYJ1Ed > > > > package main > > > > import "fmt" > > //import "time" > > > > var c = make(chan string) > > var quit = make(chan bool) > > > > func main() { > > go fn() > > > > for { > > select { > > case msg, ok := <-c: > > if !ok { > > c = nil > > return > > } else { > > fmt.Println("received: ", msg) > > quit <- true > > } > > } > > } > > } > > > > func fn() { > > for { > > select { > > case <-quit: > > fmt.Println("closin chan c") > > close(c) > > return > > //case <-time.After(time.Nanosecond): // this is ok > > default: // using default causes deadlock > > c <- "Image" > > } > > } > > } > > > > > > > > received: Image > > fatal error: all goroutines are asleep - deadlock! > > > > goroutine 1 [chan send]: > > main.main() > > /tmp/sandbox497027958/main.go:20 +0x100 > > > > goroutine 5 [chan send]: > > main.fn() > > /tmp/sandbox497027958/main.go:35 +0x40 > > created by main.main > > /tmp/sandbox497027958/main.go:10 +0x40 > > > > > > > > -- > > 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 <javascript:>. > > For more options, visit https://groups.google.com/d/optout. > -- 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.