Learning Golang: Introduction to Concurrency Patterns, goroutines and channels



Learning Golang: Introduction to Concurrency Patterns, goroutines and channels

Learning Golang: Introduction to Concurrency Patterns, goroutines and channels

Welcome! Let’s learn about Go/Golang Concurrency Patterns, but first let’s learn about concurrency in Go/Golang, including goroutines, channels and the different keywords needed: like close, range and select.

* What is a goroutine?
It is lightweight thread managed by the Go/Golang runtime, independently executed function that has its own call stack.

* What is a channel?
Is a mechanism through which we can send and receive values, it uses the arrow operator which indicates data flow.

By default sender and receiver will block if the other side is not ready

* What is a buffered channel?
It’s a type of buffer that has a length.

Sending to a buffered channel will block if it is full and receiving from a buffered channel will only block when it’s empty.

* What else can we do with channels?

There are 3 new keywords applicable to channels: “close”, “select” and “range”:

* “close” allows closing the channel, this means we can’t receive or send values to it.
* “selects” allows to receive messages from multiple channels via “cases”, it’s like a “switch”, it also supports a “default” value.
* “range” is similar to the keyword used when looping through slices/arrays; but in this cases it will continuously receive values from the channel until it gets closed.

Recall this is the “Introduction to Concurrency Patterns in Go/Golang”, in future episodes I will describe concrete patterns to use when building concurrent programs.

RELEVANT LINKS

* Previous episode: “Learning Golang: Interface Types – Part 2 (empty interface, type assertion and type switches)” https://youtu.be/m14ob5dCLag

* Example code: https://github.com/MarioCarrion/videos/tree/8b70c807d63b854cde4dcbf1a1111b856a449cd5/2021/08/12/introductions-concurrency-patterns

* Playlist “Software Architecture in Go”: https://www.youtube.com/playlist?list=PL7yAAGMOat_GCd12Lrv_evJ3Zhv1dl8B-
* Playlist “Learning Go”: https://www.youtube.com/playlist?list=PL7yAAGMOat_F7bOImcjx4ZnCtfyNEqzCy
* Playlist “Building Microservices in Go”: https://www.youtube.com/playlist?list=PL7yAAGMOat_Fn8sAXIk0WyBfK_sT1pohu
* Playlist “GoTools and Packages”: https://www.youtube.com/playlist?list=PL7yAAGMOat_HEEOvH99agDs_5g51A0Ls3
* Playlist “Testing in Go”: https://www.youtube.com/playlist?list=PL7yAAGMOat_HSeW4zF0uRL9EaHadE4ZZq

00:00 Intro
00:16 What is Concurrency?
00:36 What is Parallelism?
00:46 Analogy: Concurrency
01:53 Analogy: Parallelism
02:20 What is a goroutine in Golang?
02:49 Example: goroutine in Golang
04:03 What is channel in Golang?
05:30 Example 1: channels in Golang
06:40 Example 2: channels and goroutines in Golang
08:35 What is a buffered channel in Golang?
09:56 Example: buffered channels in Golang
11:39 Channels: Close, Select and Range in Golang
12:33 Example: goroutines and channels in Golang
17:12 Conclusion: Introduction to Concurrency Patterns in Golang

Who am I:

HelloπŸ‘‹πŸΌ! I’m Mario, a Hands-on Software Architect and Lead Backend Engineer with more than 16 years of professional experience building all kinds of software including on-premise Industrial Automation Systems, Linux Accessibility Desktop and Browser Components as well as Distributed Advertising Microservices.

Every week I will share with you different topics I’ve learned while working for small startups and large companies including the processes I’ve followed over the years for successfully delivering complex enterprise systems from start to end.

Subscribe if you like Software Development, Software Architecture and Systems Design!

Keep it up. Don’t give up!

https://twitter.com/MarioCarrion
https://instagram.com/Mario.Carrion

#golang #tutorial #concurrency

— Our affiliate links below

Shop our Amazon favorites β†’ https://www.amazon.com/shop/rubycarrion
Shop my IG feed on my LIKEtoKNOWit (LTK) page β†’ https://www.liketoknow.it/RubyCarrion
Shop Top Deals and Featured Offers at Best Buy β†’ https://bestbuy.7tiv.net/c/2558226/687081/10014
Get a 30 day FREE Trial of Epidemic Sound β†’ https://www.epidemicsound.com/referral/szx441/
Try Amazon Prime 30-day FREE Trial β†’ https://amzn.to/3yf9a0f
I love getting Cash Back and think you will too! Join for FREE and get $30 when you spend $30. https://www.rakuten.com/r/RUBYRA132?eeid=28187

— Our Vlog Channel

https://www.youtube.com/c/RubyCarrion/videos