No video

Learning Golang: Introduction to Concurrency Patterns, goroutines and channels

  Рет қаралды 44,720

Mario Carrion

Mario Carrion

Күн бұрын

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)" • Learning Golang: Inter...
* Example code: github.com/MarioCarrion/video...
* Playlist "Software Architecture in Go": • Software Architecture ...
* Playlist "Learning Go": • Learning Go/Golang
* Playlist "Building Microservices in Go": • Building Microservices...
* Playlist "GoTools and Packages": • Golang/Go Tools and Pa...
* Playlist "Testing in Go": • Testing in Golang/Go
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!
/ mariocarrion
/ mario.carrion
#golang #tutorial #concurrency
--- Our affiliate links below
Shop our Amazon favorites → www.amazon.com/shop/rubycarrion
Shop my IG feed on my LIKEtoKNOWit (LTK) page → www.liketoknow.it/RubyCarrion
Shop Top Deals and Featured Offers at Best Buy → bestbuy.7tiv.net/c/2558226/68...
Get a 30 day FREE Trial of Epidemic Sound → www.epidemicsound.com/referra...
Try Amazon Prime 30-day FREE Trial → amzn.to/3yf9a0f
I love getting Cash Back and think you will too! Join for FREE and get $30 when you spend $30. www.rakuten.com/r/RUBYRA132?e...
--- Our Vlog Channel
/ rubycarrion

Пікірлер: 38
@suprecam9880
@suprecam9880 Жыл бұрын
Hey Mario, thanks for the video! Your explanations are not hurried and barebones like most out there, and I have no trouble following along and grasping the concepts immediately. You're a great teacher.
@MarioCarrion
@MarioCarrion Жыл бұрын
Thanks for the feedback. I appreciate it, take care.
@saadati
@saadati 7 ай бұрын
Thanks for explaining the abstract concepts first before showing how they are done in Go. That helped a lot.
@worklife76
@worklife76 Жыл бұрын
Watched 5 of your videos now! These are so good, please please please keep them coming!
@MarioCarrion
@MarioCarrion Жыл бұрын
Hello. Thanks for the support. Take care.
@bench500lb4
@bench500lb4 Жыл бұрын
Excellent explanations! Thank you Mario
@michaeldausmann6066
@michaeldausmann6066 2 жыл бұрын
Hi Mario, thanks for making your content available. I hadn't come accross channels before this and the basic idea made sense. I did find the rest of the concepts it a little overwhelming tho, might come back to this after looking at some other content.
@MarioCarrion
@MarioCarrion 2 жыл бұрын
Hey Michael, thanks for watching. Take care.
@henriquealexandreh
@henriquealexandreh 3 жыл бұрын
I like your explanation. It's concise and the examples illustrate well what you mean. Thanks.
@MarioCarrion
@MarioCarrion 3 жыл бұрын
Thanks Henrique, cheers!
@fruedal
@fruedal 2 жыл бұрын
Amazing work Mario! Just found out about your channel! Keep the amazing work
@MarioCarrion
@MarioCarrion 2 жыл бұрын
Thanks Felipe! Cheers!
@ahsanjaved9524
@ahsanjaved9524 2 жыл бұрын
Thank you, Mario you explained it nicely!
@devjeff3192
@devjeff3192 Жыл бұрын
Thank you ! You explained it brilliantly
@redamadjoub
@redamadjoub Жыл бұрын
Best explanation about concurrency and parallelism. Congrats !
@MarioCarrion
@MarioCarrion Жыл бұрын
Thanks for watching! Cheers
@yuxiang4218
@yuxiang4218 6 ай бұрын
Great content! Thanks for sharing. Very helpful!
@MarioCarrion
@MarioCarrion 6 ай бұрын
Thanks for watching! I'm glad you find the context useful. Take care.
@alialiyev5892
@alialiyev5892 Жыл бұрын
At 09:50 I think text should say receiving is blocked when buffer is empty, not full. Thanks for video and explanation regardless.
@mailman2097
@mailman2097 5 ай бұрын
yes indeed..
@roberto_camp
@roberto_camp 2 жыл бұрын
fantastic!
@mohokhachai
@mohokhachai 11 ай бұрын
Cool
@tony-go-code
@tony-go-code 2 жыл бұрын
Hi Mario, first as always, thanks for your knowledgeable content. I have 1 question and it's kind of curious on how concurrency works in Docker container? are we sacrificing the concurrency if we put our go application into container? i wish it not, if so could you share how to experiment go concurrency within Docker container? hope to see your video soon on this. #go, #concurrency, #docker
@MarioCarrion
@MarioCarrion 2 жыл бұрын
That's a great question, my understanding is that in Docker concurrency depends on how we run the container, so if the container is running with limited "CPUs" then that will affect the program, I believe that also depends on the cloud provider and their own definition of "CPU". I'll run a few experiments and get back to you.
@LIFEIS702
@LIFEIS702 Жыл бұрын
Thanks great video
@MarioCarrion
@MarioCarrion Жыл бұрын
Thanks for watching, cheers
@iarbainaltainvantdevara
@iarbainaltainvantdevara 8 ай бұрын
Hi Mario and thanks for the video! One question: in the examples with select and range, you are creating a buffered ch of len 2. However, this is able to receive 5 values, not like the previous example where it would not receive anymore once the number of values sent exceeded the buffer len. How is this possible?
@MarioCarrion
@MarioCarrion 8 ай бұрын
Hi! The size of the buffered channel indicates how many values it can receive before it blocks. Receives will still block when the channel is empty. One way to demonstrate this is by updating the send in the goroutine; instead of sending one value, try sending three. You will notice it blocks after two, adding some fmt.Println will make it clearer.
@zakariachahboun
@zakariachahboun 2 жыл бұрын
good thanks
@anamikavarshney28
@anamikavarshney28 2 жыл бұрын
Hi Mario, thanks for creating such content, but I did not find much clarity in the explanation, there is a good scope for improvement, maybe it can be made more verbose and it'd be good if fumbling is reduced.
@MarioCarrion
@MarioCarrion 2 жыл бұрын
Hey Anamika, thanks for the feedback. Take care.
@IPHoldings
@IPHoldings Жыл бұрын
Good video! Only suggestion is that since you're using VIm it's difficult to follow along the line numbers.
@MarioCarrion
@MarioCarrion Жыл бұрын
Yeah, I've received that feedback before; in recent videos I'm trying to be much explicit with everything to make the content clearer. Thanks!
@thachnnguyen
@thachnnguyen 2 жыл бұрын
As always, good and to the point. However, up till this point, I still don't see a good example from your videos illustrating the difference between unbuffered and buffered channels. For example (except for the panic thing, but who does that?), I can just make the channel in the last example (05-chose-range-select) unbuffered and it works just fine. Different but fine, in the sense that the producer block transfers data to the consumer block. (The order of the print out may be out of order, but that's because I/O ops are slow, not because of something nondeterministic.) The only reason I've come across is buffered channel is async (still haven't seen a good use case that illustrates it). That is, in your example, unless the producer produces things in some nondeterministic order, there's no need to use a buffered channel.
@MarioCarrion
@MarioCarrion 2 жыл бұрын
Hello Thach, thanks for the comment. I haven't really covered the use cases regarding using buffered and unbuffered channels but you bring a good point and I think I should cover that in the near future because it's important to understand when/how to use buffered/unbuffered channels. Best
@thachnnguyen
@thachnnguyen 2 жыл бұрын
@@MarioCarrion Thanks. If you can, a video that explains the fine points of channel would be perfect. Like a goto reference when someone needs to find out something about channels. Here's why. I think Go is a really easy language, but I still don't have the confidence when it comes to channels. For example, I still haven't figured out a deadlock error message that I've got even though there doesn't seem to be any error as far as I can see. I think I still don't understand fully exactly how select or range works (my bug is probably related to it). Your coverage of the different concurrency patterns is great, btw.
@yakhoubakeita6132
@yakhoubakeita6132 2 ай бұрын
At 9:47 I guess you mean receiving will block when the channel is empty...
Learning Golang: Concurrency Patterns Fan-In and Fan-Out
13:02
Mario Carrion
Рет қаралды 9 М.
Advanced Golang: Channels, Context and Interfaces Explained
22:17
Code With Ryan
Рет қаралды 114 М.
Stay on your way 🛤️✨
00:34
A4
Рет қаралды 24 МЛН
Я обещал подарить ему самокат!
01:00
Vlad Samokatchik
Рет қаралды 8 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 15 МЛН
Concurrency in Go
18:40
Jake Wright
Рет қаралды 611 М.
Goroutines Crash Course (Mutex, Channels, Wait Group, & More!)
15:42
Master Go Programming With These Concurrency Patterns (in 40 minutes)
46:15
Golang: The Last Interface Explanation You'll Ever Need
17:58
Flo Woelki
Рет қаралды 4,5 М.
The One BIG Reason to Learn Google's Go Language
17:55
Gary Explains
Рет қаралды 156 М.
Golang Channels Or Wait Groups? Let Me Explain.
18:32
Anthony GG
Рет қаралды 20 М.
This is why Go Channels are awesome
6:06
Web Dev Cody
Рет қаралды 10 М.
Goroutines: Under the Hood | Vicki Niu | Go Systems Conf SF 2020
22:11
Stay on your way 🛤️✨
00:34
A4
Рет қаралды 24 МЛН