day10协程 串联 channel 改进
parent
d8d4475bec
commit
aed49f9d1b
|
@ -13,24 +13,37 @@ func main() {
|
||||||
close(natural) // 关闭channel
|
close(natural) // 关闭channel
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
//// squarer
|
||||||
|
//go func() {
|
||||||
|
// for {
|
||||||
|
// x, ok := <-natural
|
||||||
|
// if !ok {
|
||||||
|
// break // channel关闭,跳出循环
|
||||||
|
// }
|
||||||
|
// square <- x * x
|
||||||
|
// }
|
||||||
|
// close(square)
|
||||||
|
//}()
|
||||||
|
//
|
||||||
|
//// printer (in main goroutine)
|
||||||
|
//for {
|
||||||
|
// x, ok := <-square
|
||||||
|
// if !ok {
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
// println(x)
|
||||||
|
//}
|
||||||
|
|
||||||
// squarer
|
// squarer
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for x := range natural {
|
||||||
x, ok := <-natural
|
|
||||||
if !ok {
|
|
||||||
break // channel关闭,跳出循环
|
|
||||||
}
|
|
||||||
square <- x * x
|
square <- x * x
|
||||||
}
|
}
|
||||||
close(square)
|
close(square)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// printer (in main goroutine)
|
// printer (in main goroutine)
|
||||||
for {
|
for x := range square {
|
||||||
x, ok := <-square
|
|
||||||
if !ok {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
println(x)
|
println(x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue