https://play.golang.org/p/FMKxtVlTL5
为什么default:
select语句中的程序会无限循环?
package main
import (
"fmt"
"strconv"
"time"
)
var quit chan bool
var counter chan int
func main() {
counter = make(chan int)
quit = make(chan bool)
go func() {
i := 0
for {
i++
select {
case <-quit:
fmt.Println("Bye!")
return
case counter <- i:
fmt.Println("Send! " + strconv.Itoa(i))
default:
fmt.Println("Default! " + strconv.Itoa(i))
}
}
}()
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
fmt.Println("Receive! " + strconv.Itoa(<-counter))
quit <- true
time.Sleep(1 * time.Second)
}
隔江千里
相关分类