Letโ€™s Get Go-ing with Concurrency!

Go has very, very, good support for concurrency. No matter what programming language you are coming from, this is a good time ๐Ÿ• to sit down and read ๐Ÿ‘“ because youโ€™re going to learn something new โ€ฆ Unless youโ€™re coming from Erlang in which case youโ€™ve got concurrency down ๐Ÿ‘ and could teach me something about it ๐Ÿ˜‚ Though the code examples will still be useful to see how itโ€™s done in Go.

So first things first, Concurrency is not parallelism this is an absolutely necessary talk for any budding gopher by Rob Pike. Weโ€™ll find out with the coming lessons that concurrency is unlocked ๐Ÿ”“ with the power of goroutines, channels, condition variables and monitors ๐Ÿ•ต

Second, whatโ€™s concurrency? The ability to do tasks without blocking execution of other code. This is an incomplete definition ๐Ÿ˜“ but we need to start somewhere! Letโ€™s say you have 3 tasks you need to complete: 1. Make dinner ๐Ÿฝ๏ธ 2. Do Laundry ๐Ÿงบ 3. Watch the latest episode of your favorite show ๐Ÿ“บ How do you do all three? ๐Ÿค” Sit in front of a pot of water, wait until it boils, put in some potatoes ๐Ÿฅ” and wait for them to get soft? NO! ๐Ÿ™… I donโ€™t know about you, but for me it would look like this:

See how all three tasks got done, but none of them were ever blocking the other tasks? It was only me that was switching from each task that made it seem like all three got done at the same time (in parallel), but what actually happened is when I was waiting for something to finish, I didnโ€™t just stand there ๐Ÿง I picked up another task.

I did not do all my tasks at the same time, I did them when they were ready to move to their next steps โ€“ this is concurrency. If I did each one of them at the same time (Letโ€™s say I had two friends come over) then it would be parallelism. We can see that in this case ๐Ÿ’ผ Having a friend watch water boil and another friend watch the washer until it turns off wouldnโ€™t make the process any quicker

We can see when I was alone dealing with all my tasks, I got them done in the same time it would take for three ๐Ÿง‘๐Ÿง”๐Ÿ‘ฉ of us to do each task separately. Thatโ€™s the difference with concurrency and parallelism; dealing (concurrency) with many things versus doing (parallelism) many things. Concurrency is not parallelism. It allows for parallelism.

And as a final note, many of the lessons will be going into heavy detail around concurrency. There are still plenty of coding examples, but itโ€™s important to recognize the ideas and patterns of concurrency, before you jump in head first ๐ŸŠ Concurrency is a notoriously difficult topic to handle.

Just because Go makes it incredibly easy to use concurrency, doesnโ€™t mean Go will make concurrency any easier. Meaning, itโ€™s easy to blame your faults on the tool, instead of admitting your depth of knowledge and lack of understanding is the reason the application is failing. After all, you wouldnโ€™t blame a chainsaw โ›“๏ธ๐Ÿชš for cutting the table in half when you tried to cut the butter ๐Ÿงˆ would you? ๐Ÿ˜ You have full knowledge ๐Ÿง  of when and where to use a chainsaw. Now letโ€™s Go ๐Ÿ˜น make that a reality with concurrency! ๐Ÿ’ช๐Ÿ˜