Jason Whatson

  • Git hub
  • Twitter
  • Skype
  • Linkedin
  • Instagram
  • STAY CONNECTED

Software engineer - Web & Mobile development, cloud computing : Sydney, AU;
  • About me
  • My resume
  • Contact
  • My Blog
  • Quotes
  • My portfolio’s
    • Software portfolio
    • Graphics portfolio
    • Music production & recording

An example of how Golang makes concurrent programming easy and awesome

  • Jan 16, 2014

     

    Cute Gopthers burning old c books concurrently – Source Rob Pike

     

    With todays multicore processors writing concurrent code is very beneficial and the designers of Golang have made the concurrency model in Golang extremely easy and simple  to implement in your code, as we will now see in the example below.

    Just a note on the above mention of multicore CPU’s. Just because the system has more than 1 core and you write concurrent code it does not necessarily  mean  it will run across more than one core. To see why this is and to also understand why ‘Concurrency Is Not Parallelism’ you should watch Rob Pikes talk here. Heck even if you don’t plan on programming Go you should probably watch it anyway.

    Example

    Go
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    package main
     
    import (
        "fmt"
        "sync"
        "time"
    )
     
    func f(wg *sync.WaitGroup, val string) {
        time.Sleep(3 * time.Second) //Sleep a few secs
        fmt.Printf("Finished: %v - %v\n", val, time.Now())
        wg.Done()
    }
     
    func main() {
     
        var wg sync.WaitGroup
     
        wg.Add(3) //We need to wait for 3 calls to 'done' on this wait group
     
        go f(&wg, "goroutine A") //Call function f concurrently
     
        //Anonymous func / closure
        go func(wg *sync.WaitGroup, val string) {
            time.Sleep(3 * time.Second) //Sleep a few secs
            fmt.Printf("Finished: %v - %v\n", val, time.Now())
            wg.Done()
        }(&wg, "goroutine B") //Call Anonymous function concurrently
     
        go f(&wg, "goroutine C") //Call function f concurrently
     
        wg.Wait() //Wait for the concurrent routines to call 'done'
     
        fmt.Printf("Finished all goroutines: %v\n", time.Now())
    }

     Code analysis

    Calling a function concurrently is really simple. We just use the ‘go’ keyword before the call

    Go
    1
    go f(&wg,"goroutine C")

    However usually we when we have multiple things happening concurrently we want to make sure that the individual processes synchronize  together at some point in the programs execution.

    For example in a web page backed by a database, we might want to make multiple database queries concurrently, but we need to wait until they have all returned their datasets before we render the data in HTML and send the page over the wire to the user.

    Fortunately this is quite easy using the Sync package

    First we define how many go routines we will be waiting for until we release the block

    Go
    1
    wg.Add(3)

    Then at the end of our concurrent go routine we need to tell sync that we have completed one of the tasks, which will decrement the waiting counter.

    1
    wg.Done()

    Finally we need to tell the program where to block. The code after the call to wait() will not execute until all go routines have finished their tasks by them indicating so – by calling done()

    Go
    1
    wg.Wait() //Wait for the concurrent routines to call 'done'

    If you run this example, you will see that each call to the go routined function in main() get’s called and then moves on to the next goroutine call. Rather than sequentially calling goroutine A then goroutine B and goroutine C the function calls happen concurrently.

    This is show by waiting in our goroutine for 3 seconds. Once the program executes it waits 3 seconds then all our called go routines will print their output together. If we didn’t use concurrency the program would wait 3 secs then print its first output, then wait another 3 seconds (6 seconds in total) and then print its output and finally wait again and finally finish after 9 seconds.

    You might also notice that we can use call go routines on an anonymous function / closure. This is another of Go’s awesome features (first class functions).

    Go
    1
    2
    3
    4
    5
    go func(wg *sync.WaitGroup, val string) {
       time.Sleep(3 * time.Second) //Sleep a few secs
       fmt.Printf("Finished: %v - %v\n", val, time.Now())
       wg.Done()
    }(&wg, "goroutine B")

    Note: This is not the only way to sync in Go. For a post on channels vs the above method please read – channels-vs-sync-package. I personally find the sync package cleaner

    Conclusion

    Go makes concurrency both clean and simple. Abstracting away all the complexities which other languages don’t. Because of this, there is no reason in this day and age you should not be writing concurrent code where you can

    Golang
Jason Whatson

Jason Whatson

Jason is a software developer from Sydney Australia who loves to write software to build great products and help businesses succeed with their goals. Jason is a aspiring entrepreneur who subscribes to the lean startup ideology.

7 Comments

  1. Hobby

    −Reply
    February 13th, 2014

    thx for posting, really helpful.

  2. tile kitchen backsplash Craftsbury Common VT

    −Reply
    March 24th, 2015

    It is best to participate in a contest for probably the greatest blogs on the web. I’ll advocate this site!

  3. Menno SD dog door

    −Reply
    March 24th, 2015

    Very good blog! Do you have any recommendations for aspiring writers? I’m hoping to start my own site soon but I’m a little lost on everything. Would you recommend starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m completely overwhelmed .. Any recommendations? Thanks!

  4. colefax and fowler discount fabrics

    −Reply
    June 5th, 2016

    Hi friends, how is all, and what you wish for to say on the topic of this
    article, in my view its in fact awesome for me.

  5. CollinJBudhu

    −Reply
    October 7th, 2016

    Everyone loves it when individuals come together and share
    opinions. Great website, continue the excellent work!

  6. sandracn18

    −Reply
    August 29th, 2018

    Very recently started supplementary occupation:
    http://pat.w.telrock.org

Leave A Reply

Leave a Reply Cancel reply

Recent Posts

  • Book Review: Unnatural Selection: Why the Geeks will Inherit the Earth – Mark Roeder
  • Incremental backups to Amazon S3 on CentOS using duplicity
  • An example of how Golang makes concurrent programming easy and awesome
  • Installing NGINX + PHP 5.5 with opcache + MySql on CentOS 6 (LEMP)
  • Basic software engineering principles – Don’t Repeat Yourself (DRY)

Recent Comments

  • freelancerinseo.com on Golang vs JAVA vs PHP: Comparing productivity
  • sandracn18 on An example of how Golang makes concurrent programming easy and awesome
  • john on Book Review: Unnatural Selection: Why the Geeks will Inherit the Earth – Mark Roeder
  • Chaos Orbs on Golang vs JAVA vs PHP: Comparing productivity
  • Kris on Golang vs JAVA vs PHP: Comparing productivity

Archives

  • March 2014
  • February 2014
  • January 2014
  • November 2013
  • June 2013
  • April 2013
  • May 2010

Categories

  • Book Reviews
  • Programming
  • Software engineering
  • Web hosting

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

About me

I am a software developer from Sydney Australia who loves to write software to build great products and help businesses succeed with their goals.

I am an aspiring entrepreneur who subscribes to the lean startup ideology and creator of “positive affirmations”, a app for Android and iPhone.

Contact

Phone: 0439372293
Email: jason@mindfsck.net

Random Quote

He who sacrifices freedom for security deserves neither — Benjamin Franklin

© Copyright 2019 Jason Whatson. All Rights Reserved.