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:
29
eBook/examples/chapter_14/channel_idiom2.go
Normal file
29
eBook/examples/chapter_14/channel_idiom2.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
suck(pump())
|
||||
time.Sleep(1e9)
|
||||
}
|
||||
|
||||
func pump() chan int {
|
||||
ch := make(chan int)
|
||||
go func() {
|
||||
for i := 0; ; i++ {
|
||||
ch <- i
|
||||
}
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
func suck(ch chan int) {
|
||||
go func() {
|
||||
for v := range ch {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user