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

Popular posts from this blog

Fixing the DeepSpeed Import Error While Fine-Tuning the Qwen Model

Amazon Linux 2023 - User data configuration for launch templates to connect to the EKS cluster

How to create ISM policy and rotate logs in opensearch