Go routine and how to use chan to get task' result
you should forget waitGroup :-)
naturals := make(chan int) squares := make(chan int) go func() { for x := 0; ; x++ { naturals <- x } }() go func() { for { x := <-naturals
squares <- x * x
} }() for { fmt.Println(<-squares) }
The above code is using unbuffered chan int. It means that that when naturals <-x
then it must wait for getting x out by calling <-naturals then we can continue
push naturals <-x. It is easy to say that it is not performance.
In the next tutorial we will use buffered chan
Comments
Post a Comment