<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Film Parichaya</title>
    <description>The latest articles on DEV Community by Film Parichaya (@filmptz).</description>
    <link>https://dev.to/filmptz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F647569%2F0c9e0611-df80-4dbf-b799-7362659b856e.jpeg</url>
      <title>DEV Community: Film Parichaya</title>
      <link>https://dev.to/filmptz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/filmptz"/>
    <language>en</language>
    <item>
      <title>Synchronization multiple goroutines with  sync.WaitGroup and Channel</title>
      <dc:creator>Film Parichaya</dc:creator>
      <pubDate>Sat, 12 Jun 2021 12:39:43 +0000</pubDate>
      <link>https://dev.to/filmptz/synchronization-multiple-goroutines-with-sync-waitgroup-and-channel-2mmo</link>
      <guid>https://dev.to/filmptz/synchronization-multiple-goroutines-with-sync-waitgroup-and-channel-2mmo</guid>
      <description>&lt;p&gt;Golang has support for concurrency using goroutines and channels.&lt;br&gt;
In this article, we will talk about how to synchronize multiple goroutines with &lt;code&gt;sync.WaitGroup&lt;/code&gt; and &lt;code&gt;channel&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sync.WaitGroup&lt;/strong&gt;&lt;br&gt;
Used to wait for goroutines to finish executing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Add(delta int) // how many goroutines that you want to wait
Done()         // call when goroutine finished, this method will reduce the number of goroutines waiting
Wait()         // wait for all goroutines finisied then do next function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Channel&lt;/strong&gt;&lt;br&gt;
Channel is a pipeline for sending and receiving data between differenct goroutines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;channel := make(chan &amp;lt;type&amp;gt;) // declare channel variable
chanel &amp;lt;- s_value            // send value into a channel
r_value := &amp;lt;-chanel          // receive a value from the channel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Let's Code!&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main(){
    var wg sync.WaitGroup
    var shared int
    channel := make(chan bool)

    wg.Add(3)
    go func() {
        defer wg.Done()
        shared = rand.Intn(100)
        close(channel)
        fmt.Printf("Task1 Done | SET shared = %d ... \n",shared)
    }()

    go func ()  {
        defer wg.Done()
        //wait for Task1
        &amp;lt;-channel
        fmt.Printf("Task2 Done | GET shared = %d ... \n",shared)
    }()

    go func () {
        defer wg.Done()
        //wait for Task1
        &amp;lt;-channel
        fmt.Printf("Task3 Done | GET shared = %d ... \n",shared)
    }()

    wg.Wait()
    fmt.Println("All done!")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; go run main.go
Task1 Done | SET shared = 81 ... 
Task2 Done | GET shared = 81 ... 
Task3 Done | GET shared = 81 ...
All done!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How this code work?&lt;/strong&gt;&lt;br&gt;
this code is the program that contains 3 goroutines, the first goroutine is executed first to set the value to share variables and the last 2 goroutines are wait for the shared variable to be set then print the shared variable.&lt;/p&gt;

</description>
      <category>go</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
