If you have come across with this story, you have reached lots of readings about the Concurency. It is possible to find many contents on Google. But the main subject is find to well structered articles. If you can’t clarify the Concurency in your mind after lots of readings, you are in right place to learn. Gophers! Let’s get start!

Code Study Blog

Is Parallelism a Concurrency?

On general meaning, you can see the explains on web like that “Concurrency is the task of running and managing the multiple computations at the same time. While parallelism is the task of running multiple computations simultaneously”. But it is not clear right?

Pradeep Singh, The Geeks Diary

Parallelism is related to systems. Parallelism is obtained by using multiple CPUs. If you have enough resources, you can provide parallelism. For example, if we write a code with intent that two chunks of the program will run in parallel, does anyone can guarantee it work as a parallel ? If we run the code with machine that has one core? Answer is No! It appears running as a parallel but it executes as sequential.

Concurrency is related to the context. It executes like that not waiting for one thing to finish to start other. It works with particularly context switching. We don’t have a chance to know which context will start. It gives us a flexibility regardless of the system.

To sum up “Concurrency is a property of the code; parallelism is a property of the running program”. So the concurrency and parallelism are not exactly same things.


What is the Goroutine?

Goroutine is a way of the executing the concurrency in Golang. “A goroutine is a lightweight execution thread in the Go programming language and a function that executes concurrently with the rest of the program”. It really easy to do Goroutine. You just invoke a function as a goroutine, use the go keyword.

By the way, “Go relies on a concurrency model called CSP ( Communicating Sequential Processes) , which -in computer science- is basically a model that describes interactions between concurrent systems”. If you would like to get lost in details just click it.

package mainimport (
)// Prints numbers from 1-3 along with the passed string
func foo(s string) {
for i := 1; i <= 3; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s, ": ", i)
}func main() {// Starting goroutine
go foo("goroutine")fmt.Println("Main goroutine finished")

But why Goroutine is not worked? What if I add time.Sleep(time.Second) on main?

func main() {// Starting goroutinego foo("goroutine")// Wait for goroutine to finish before main goroutine endstime.Sleep(time.Second)fmt.Println("Main goroutine finished")}

Yes! It worked. But how?https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3o7btPCcdNniyf0ArS%2Ftwitter%2Fiframe&display_name=Giphy&url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3o7btPCcdNniyf0ArS%2Fgiphy.gif&image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3o7btPCcdNniyf0ArS%2Fgiphy.gif&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=giphy

Actually it is related that the model of concurrency that Golang use. The main function runs as a goroutine. So on the first example (without time.Sleep()) the main doesn’t wait for the go foo() to runs. But on the second one, with time.Sleep(time.Second) we just give a second before the program finished. And it worked! But every time we can’t give a time guess that tells us how long the program takes. Because the Goroutines that we use in our programs are not basic that works in a second. So how can we control the Goroutines?

Concurrency in Go

“Go follows a model of concurrency called the fork-join model. The word fork refers to the fact that at any point in the program, it can split off a child branch of execution to be run concurrently with its parent. The word join refers to the fact that at some point in the future, these concurrent branches of execution will join back together. Where the child rejoins the parent is called a join point”. So we have to control the fork and join process.

Don’t Communicate By Sharing Memory!!!

There are lots of way that you can control the fork and join process. But there are two main way in golang. One of them is that we call as lower-level synchronization. You can use the lower-level synchronization techniques that helps you lock memory to not allowing others to read and write on it until the communication is complete. They are in sync package like WaitGroup, Mutex, RWMutex, Cond, Once, Pool. If it is not use in proper way, lower-level synchronization techniques can causes some issues like Race Conditions, Deadlocks, Livelocks, and Starvation because of wrong usage.

On the other hand, we have the higher-level synchronization. You can use the higher-level synchronization techniques to notify sender and receiver via channel rather than blocking the memory. You can use Channels and Select for higher-level synchronization.

Don’t communicate by sharing memory, share memory by communicating.

How Can I Select The Right Techniques?

There are lots of speculations about the usage of concurrency in Golang. But there is quote from the Go Wiki on the matter:

One of Go’s mottos is “Share memory by communicating, don’t communicate by sharing memory.”

That said, Go does provide traditional locking mechanisms in the sync package. Most locking issues can be solved using either channels or traditional locks.

So which should you use?

Use whichever is most expressive and/or most simple.

A common Go newbie mistake is to over-use channels and goroutines just because it’s possible, and/or because it’s fun. Don’t be afraid to use a sync.Mutex if that fits your problem best. Go is pragmatic in letting you use the tools that solve your problem best and not forcing you into one style of code.

Concurrency in Go

I hope that I could give some insights to you about the concurrency. Concurrency can be a pain in the neck in your code if it doesn’t used well. Okay Gophers! Just Go away or Just know that Go is the way to Go!