basics: use two channels to control the workflow.

This commit is contained in:
oscarzhou 2021-04-25 22:52:57 +12:00
parent c7bc4d0670
commit bdd898be69

45
basics/main.go Normal file
View File

@ -0,0 +1,45 @@
package main
import "fmt"
func walk(ch1, ch2 chan int) {
fmt.Println("walk")
fmt.Println("1")
ch1 <- 2
fmt.Println("2")
y := <-ch2
fmt.Println("y=", y)
fmt.Println("3")
}
func loadWorker(ch1, ch2 chan int) {
fmt.Println("loadWorker")
fmt.Println("4")
select {
case x := <-ch1:
fmt.Println("x=", x)
fmt.Println("5")
select {
case ch2 <- x + 5:
fmt.Println("6")
}
}
fmt.Println("7")
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
go loadWorker(ch1, ch2)
walk(ch1, ch2)
fmt.Println("Done")
}