I got these code. not gonna work in play (play error: take too long to process )
package main
import "fmt"
var i = 0
func httpHandler() {
errCh := make(chan error, 1)
resultCh := make(chan int, 1)
go func() {
defer close(errCh)
defer close(resultCh)
errCh <- fmt.Errorf("shit")
}()
select {
case <-errCh:
case <-resultCh:
i++
println(i, " this shall not happen dude!")
}
}
func main() {
for i := 0; i < 1000000; i++ {
httpHandler()
}
}
run first time:
1 this shall not happen dude!
2 this shall not happen dude!
3 this shall not happen dude!
4 this shall not happen dude!
5 this shall not happen dude!
6 this shall not happen dude!
7 this shall not happen dude!
run second time:
1 this shall not happen dude!
2 this shall not happen dude!
3 this shall not happen dude!
4 this shall not happen dude!
5 this shall not happen dude!
6 this shall not happen dude!
7 this shall not happen dude!
8 this shall not happen dude!
9 this shall not happen dude!
10 this shall not happen dude!
11 this shall not happen dude!
12 this shall not happen dude!
13 this shall not happen dude!
14 this shall not happen dude!
15 this shall not happen dude!
this line should never been printed, but when i run, every time it get printed some times, the number of times appeared is also uncertain.
so, what’s going on? can someone explain these?