basics: use two channels to control the workflow.
This commit is contained in:
		
							parent
							
								
									c7bc4d0670
								
							
						
					
					
						commit
						bdd898be69
					
				
							
								
								
									
										45
									
								
								basics/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								basics/main.go
									
									
									
									
									
										Normal 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")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user