mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-11-13 17:36:12 +08:00
update book code
This commit is contained in:
31
eBook/exercises/chapter_14/goroutine_select.go
Executable file
31
eBook/exercises/chapter_14/goroutine_select.go
Executable file
@@ -0,0 +1,31 @@
|
||||
// Q20b_goroutine.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func tel(ch chan int, quit chan bool) {
|
||||
for i:=0; i < 15; i++ {
|
||||
ch <- i
|
||||
}
|
||||
quit <- true
|
||||
}
|
||||
|
||||
func main() {
|
||||
var ok = true
|
||||
ch := make(chan int)
|
||||
quit := make(chan bool)
|
||||
|
||||
go tel(ch, quit)
|
||||
for ok {
|
||||
select {
|
||||
case i:= <-ch:
|
||||
fmt.Printf("The counter is at %d\n", i)
|
||||
case <-quit:
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user