case
expressions of switch
code blocks (in Go toolchain v1.20 - v1.23.x)
// chan-cmp-bug.go
package main
import "fmt"
func main() {
x := make(chan struct{})
var y <-chan struct{} = x
switch x {
case y: fmt.Print(1)
default: fmt.Print(0)
}
switch y {
case x: fmt.Print(1)
default: fmt.Print(0)
}
fmt.Println(x == y)
}
11true
. But it prints 01true
with Go toolchain versions from v1.20 to v1.23.0 (exclusive).
$ gotv 1.19. run chan-cmp-bug.go [Run]: $HOME/.cache/gotv/tag_go1.19.13/bin/go run chan-cmp-bug.go 11true $ gotv 1.20 run chan-cmp-bug.go [Run]: $HOME/.cache/gotv/tag_go1.20/bin/go run chan-cmp-bug.go 01true $ gotv 1.21. run chan-cmp-bug.go [Run]: $HOME/.cache/gotv/tag_go1.21.12/bin/go run chan-cmp-bug.go 01true $ gotv 1.22. run chan-cmp-bug.go [Run]: $HOME/.cache/gotv/tag_go1.22.5/bin/go run chan-cmp-bug.go 01true $ gotv 1.23.0 run chan-cmp-bug.go [Run]: $HOME/.cache/gotv/tag_go1.23rc1/bin/go run chan-cmp-bug.go 11true
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.