mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-12-01 17:36:09 +03:00
32 lines
590 B
Go
32 lines
590 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
"syscall"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
ch := make(chan int, 64) // 成果队列
|
||
|
go Producer(3, ch) // 生成 3 的倍数的序列
|
||
|
go Producer(5, ch) // 生成 5 的倍数的序列
|
||
|
go Consumer(ch) // 消费 生成的队列
|
||
|
// Ctrl+C 退出
|
||
|
sig := make(chan os.Signal, 1)
|
||
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
||
|
fmt.Printf("quit (%v)\n", <-sig)
|
||
|
}
|
||
|
|
||
|
func Producer(factor int, out chan<- int) {
|
||
|
for i := 0; ; i++ {
|
||
|
out <- i * factor
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func Consumer(in <-chan int) {
|
||
|
for v := range in {
|
||
|
fmt.Println(v)
|
||
|
}
|
||
|
}
|