Go came to existance to solve problems being faced by today’s software, one of them is concurrency (ability to execute stuff in parallel while providing control and communication among them). To solve this go has provided goroutines to execute stuff in parallel.
By default, main function of package in go is run concurrently, implicitly. For expliit call we pass code block(that needs to be executed in routine) to go
Notice fmt.Scanln(&get)
; this actually makes our main program to wait for inputs, otherwise it will exit before go routine actualy can print. A problem with this code is that, it looks synchronous, in order. To actually see that if goroutine are asynchronous, we need to make goroutine sleep for random time. Lets rewrite this as:
Look at this, now it is random. This illustrates that go routine are independent to each other and runs separately.
Now what we showcased so far is called parallelism, we still need to make them communicate to each other and channels
comes to our rescue. Channels provide a mechanism of communication and synchronisation among goroutines. Lets look at example.
That’s interesting. This is just a simple example of channels
capabilities, there is much more.
About The Author
I am Pankaj Baagwan, a System Design Architect. A Computer Scientist by heart, process enthusiast, and open source author/contributor/writer. Advocates Karma. Love working with cutting edge, fascinating, open source technologies.
To consult Pankaj Bagwan on System Design, Cyber Security and Application Development, SEO and SMO, please reach out at me[at]bagwanpankaj[dot]com
For promotion/advertisement of your services and products on this blog, please reach out at me[at]bagwanpankaj[dot]com
Stay tuned <3. Signing off for RAAM