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 naturals
Then it can pushing to this chan maximum 10 element without waiting for getting out.

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