<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amit on Medium]]></title>
        <description><![CDATA[Stories by Amit on Medium]]></description>
        <link>https://medium.com/@ammit?source=rss-9d1d7f8ceb9f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*FYi28jgUpyTxysz8B_bxKg.png</url>
            <title>Stories by Amit on Medium</title>
            <link>https://medium.com/@ammit?source=rss-9d1d7f8ceb9f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 12:23:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ammit/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using WaitGroups in Golang]]></title>
            <link>https://medium.com/@ammit/using-waitgroups-in-golang-with-example-3adbcd9c7330?source=rss-9d1d7f8ceb9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3adbcd9c7330</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[goroutines]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Amit]]></dc:creator>
            <pubDate>Sun, 12 Apr 2020 19:14:15 GMT</pubDate>
            <atom:updated>2020-04-12T19:14:15.245Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jMnvoqSbr0IrwBEyQ4dIMg.png" /></figure><h3>Using WaitGroups in Golang with example</h3><p>When we create a goroutine, they always run in the background and the control is immediately returned to the main function. If the main function exits, that goroutine will not be executed thoroughly.</p><p>Let’s take the following example using goroutines. The main function continued without waiting for the completion of goroutines.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d069ad7f2e77212edbb993804593380/href">https://medium.com/media/1d069ad7f2e77212edbb993804593380/href</a></iframe><p>Fortunately, Golang provides a package named <strong>sync</strong> that provides basic synchronization methods that can help us solve the above issue. <strong>WaitGroup</strong> is one of them. Let’s see what the WaitGroup is and how it help us solve the above scenario.</p><p>Fortunately, Golang provides a package named <strong>sync</strong> that provides basic synchronization methods that can help us solve the above issue. <strong>WaitGroup</strong> is one of them. Let’s see what the WaitGroup is and how it help us solve the above scenario.</p><h3>What is WaitGroup?</h3><p>If you take a look at the documentation, It says WaitGroup waits for a collection of goroutines to finish. WaitGroup uses a simple counter-based lock mechanism to block the main program until all concurrently running goroutines are finished.</p><p>It provides different methods like — Add, Done and Wait.</p><h4>Add (delta int)</h4><ul><li>It takes an integer value as an argument, The argument can be negative, and that will act as <em>Done().</em></li><li>It increments the WaitGroup counter by the provided value.</li><li>It must be called before the statement creating the goroutine to prevent race conditions.</li></ul><h4>Done ()</h4><ul><li>It doesn’t take any argument.</li><li>It means that one goroutine has finished its execution, and it decrements WaitGroup counter by 1</li><li>It must execute at the end of a function inside a goroutine.</li></ul><h4>Wait ()</h4><ul><li>It blocks the main program until the WaitGroup counter goes back to 0; which means all goroutines have finished their execution.</li></ul><p>Here is an example after using WaitGroup.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f43ce009b760181c8defa208d31caf82/href">https://medium.com/media/f43ce009b760181c8defa208d31caf82/href</a></iframe><p>You can observe that the main process now waits for all the goroutines to finish their executions.</p><p>You might have noticed that we have created a <strong><em>reference</em></strong> to WaitGroup at the beginning of the main function. That is to avoid copying WaitGroup while passing it around the application.</p><p>Inside the for loop, we have called wg.Add(1) to increment the WaitGroup counter by one. Inside the function itself, we are using wg.Done() to tell WaitGroup that the current goroutine gas finished its execution.</p><p>Then at the end of our application, we called wg.Wait() which blocks the main process until the WaitGroup counter becomes 0again.</p><h4>References</h4><ul><li><a href="https://golang.org/pkg/sync/#WaitGroup">https://golang.org/pkg/sync/#WaitGroup</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3adbcd9c7330" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>