mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:34:15 +08:00
27 lines
378 B
Go
Executable File
27 lines
378 B
Go
Executable File
// Q20_goroutine.go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
func tel(ch chan int) {
|
|
for i := 0; i < 15; i++ {
|
|
ch <- i
|
|
}
|
|
close(ch) // if this is ommitted: panic: all goroutines are asleep - deadlock!
|
|
}
|
|
|
|
func main() {
|
|
var ok = true
|
|
var i int
|
|
ch := make(chan int)
|
|
|
|
go tel(ch)
|
|
for ok {
|
|
if i, ok = <-ch; ok {
|
|
fmt.Printf("ok is %t and the counter is at %d\n", ok, i)
|
|
}
|
|
}
|
|
}
|