Go routine and how to use chan to get task' result with buffered chan P2
naturals := make(chan int,10) squares := make(chan int) go func() { for x := 0; ; x++ { fmt.Println("Pushing") naturals <- x } }() go func() { for { fmt.Println("Getting out x") x := <-naturals
squares <- x * x
time.Sleep(time.Second*2) } }() for { fmt.Println(<-squares) }
naturals := make(chan int,10)As you can see that we use a buffered chan int with size 10 for naturalsThen it can pushing to this chan maximum 10 element without waiting for getting out.
Comments
Post a Comment