66 Star 402 Fork 483

infraboard/go-course

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pipeline.go 910 Bytes
一键复制 编辑 原始数据 按行查看 历史
Mr.Yu 提交于 2021-08-08 18:53 +08:00 . 更新课件
package cspmodel
import (
"fmt"
"math/rand"
"sync"
)
var wg sync.WaitGroup
func PipelineMode() {
wg.Add(3)
// 创建两个channel
ch1 := make(chan int)
ch2 := make(chan int)
// 3个goroutine并行
go getRandNum(ch1)
go addRandNum(ch1, ch2)
go printRes(ch2)
wg.Wait()
}
func getRandNum(out chan<- int) {
// defer the wg.Done()
defer wg.Done()
var random int
// 总共生成10个随机数
for i := 0; i < 10; i++ {
// 生成[0,30)之间的随机整数并放进channel out
random = rand.Intn(30)
out <- random
}
close(out)
}
func addRandNum(in <-chan int, out chan<- int) {
defer wg.Done()
for v := range in {
// 输出从第一个channel中读取到的数据
// 并将值+1后放进第二个channel中
fmt.Println("before +1:", v)
out <- (v + 1)
}
close(out)
}
func printRes(in <-chan int) {
defer wg.Done()
for v := range in {
fmt.Println("after +1:", v)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/infraboard/go-course.git
git@gitee.com:infraboard/go-course.git
infraboard
go-course
go-course
20dd46295900

搜索帮助