You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import "fmt"
import "time"
func main() {
ch := make(chan int)
ch2 := make(chan int)
ch3 := make(chan int)
go func() {
select {
case i := <-ch:
fmt.Println("Received", i, "from main channel")
case i := <-ch2:
fmt.Println("Received", i, "from ch2, how odd")
}
}()
go func() {
select {
case ch <- 1:
fmt.Println("Written 1 to main channel")
case ch3 <- 1:
fmt.Println("Written 1 to ch3, how odd")
}
}()
time.Sleep(100)
}
The runtime figures out which channel operation can proceed, and it does:
Written 1 to main channel
Received 1 from main channel
From checking the source code, it looks like performing a select never adds anything to the channels which are ready to read or to write, only an unconditional ch.send() or ch.recv() will do that, so it's not possible for two selects on either end of an unbuffered channel to ever proceed.
This is a pretty big departure from how unbuffered channels work in go. It seems that this is either a bug to be fixed, or should be mentioned in the documentation.
The text was updated successfully, but these errors were encountered:
Hi cleatoma, you're right, this is definitely not working correctly.
It should be pretty easy to reproduce this as a test case, if you want to try it out?
I don't really have the time to fix this right now as I'm not using goless actively anymore, but would happily review and merge any PRs.
I had to port recently some of my go code to python. And for that I needed properly working select. Knowing about goless I initially wanted to use it, but in the end came up with my own channels implementation:
This go program works:
The runtime figures out which channel operation can proceed, and it does:
Written 1 to main channel
Received 1 from main channel
The equivalent goless program doesn't work:
Nothing is printed.
From checking the source code, it looks like performing a select never adds anything to the channels which are ready to read or to write, only an unconditional ch.send() or ch.recv() will do that, so it's not possible for two selects on either end of an unbuffered channel to ever proceed.
This is a pretty big departure from how unbuffered channels work in go. It seems that this is either a bug to be fixed, or should be mentioned in the documentation.
The text was updated successfully, but these errors were encountered: