Go came to existance to solve problems being faced by today’s software, one of them is concurrency (ability to execute stuff in parellel 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.