Go sync.WaitGroup Basics - tutorial

  Рет қаралды 23,996

TutorialEdge

TutorialEdge

5 жыл бұрын

👨‍💻 For the price of a ☕ every month, sign up and gain access to a growing list of premium courses on my site - tutorialedge.net/pricing/ 👩‍💻
● SUBSCRIBE to see more of my Videos & hit that LIKE button to support the channel!
Text Tutorial: tutorialedge.net/golang/go-wa...
If you are just starting your journey about learning Go and how to implement highly concurrent, high-performance applications, then an understanding of WaitGroups is vital.
In this tutorial, we are going to be covering the following:
* What WaitGroups are and when we should use them
* A simple example of working with WaitGroups
* A real world example of WaitGroups
By the end of this, you should have a solid grasp as to how to employ WaitGroups within your own concurrent Go applications.
‎● My Twitter ► ⁦‪ / elliot_f
‎● My Website ► tutorialedge.net

Пікірлер: 31
@richdobbs6595
@richdobbs6595 4 жыл бұрын
With the first example, you are tightly coupling the synchronization with the underlying functionality. I'm a noobie with Go, but it seems to me better to leave the base func unchanged, and do the wg.Done() within a closure func used with the go statement. This is better separation of concerns, while keeping the wait group code nicely grouped in one place in the code.
@Tutorialedge
@Tutorialedge 4 жыл бұрын
This is a very good point, and I am going to pin this for others to see. Not only does it separate the logic, but decoupling also makes it a little simpler to test so that you don't have to create WaitGroups within your Test functions. Thanks for highlighting this!
@a_maxed_out_handle_of_30_chars
@a_maxed_out_handle_of_30_chars Жыл бұрын
can you explain this a bit simpler by showing the code?
@yevgeniyv.87
@yevgeniyv.87 5 жыл бұрын
Another great video. I really like how you added in the visuals, even though its a simple drawing it really helps especially in concepts pertaining to concurrency.
@blaxtapa
@blaxtapa 5 жыл бұрын
This is actually great. I'm quite new to go and used to be confused by go routines, channels and waitgroups. But your videos explain that very well. Thanks!
@jk.g
@jk.g 4 жыл бұрын
Great video - especially loved the real-world example at the end. Thanks
@Khisant1
@Khisant1 5 жыл бұрын
Your tutos are awesome, keep working dude!
@anothercrappypianist
@anothercrappypianist 5 жыл бұрын
Excellent little tutorial. To the point, covers the basics, plus a very common use-case. And as a personal bonus, a long history of following Jim at AdoredTV has very nicely prepared me to watch this at 1.5x.
@shazeb6278
@shazeb6278 4 жыл бұрын
Really really good tutorial mate. Loves your explanation.
@Tutorialedge
@Tutorialedge 4 жыл бұрын
Glad you liked it!
@GabrielGongoraNavarrete
@GabrielGongoraNavarrete Жыл бұрын
Thank you very much, this a best tutorial wg
@UAStriker
@UAStriker 5 жыл бұрын
Lessons about gRPC and ProtoBuf, please
@Tutorialedge
@Tutorialedge 5 жыл бұрын
I'll add it to the todo list! Thank you for your suggestion!
@faaaauuuukyou
@faaaauuuukyou 4 жыл бұрын
@@Tutorialedge super friendly nudge.... i've found only kzfaq.info/get/bejne/hNV0hdRnud3PpKc.html
@web3tel
@web3tel 2 жыл бұрын
thanks for the tutorial. If err != nil, can we still access resp as implemented here?
@JoeEnos
@JoeEnos Жыл бұрын
Seems like you could do the same thing with channels. Do you have a particular way of deciding which way is more appropriate for a given use case? Or is it just whatever is clearer for the scenario?
@antoniothomacelli
@antoniothomacelli 2 жыл бұрын
Thank you. What name of this software did you use to draw on the right?
@keanuf
@keanuf 4 жыл бұрын
Thank You
@usmanusmonov2340
@usmanusmonov2340 3 жыл бұрын
Thanks!
@rfdangel001
@rfdangel001 5 жыл бұрын
Awesome tutorial, didn't use waitgroups yet, one small issue though, shouldn't a return be set after fmt.Fprintf(w, "%+v ", err) as the execution will continue and return the status, and might cause an error in the program or even a crash if the http.Get failed?
@Tutorialedge
@Tutorialedge 5 жыл бұрын
I don’t believe it’s necessary. If you have a look at the official documentation on the go site, they don’t explicitly add a return statement. I haven’t yet faced any issue with this way in any of the production systems that I have deployed over the years so you should be safe! I hope this helps! 😊
@rfdangel001
@rfdangel001 5 жыл бұрын
@@Tutorialedge I see, didn't know that, thanks for the clarification :)
@gagansingh4888
@gagansingh4888 3 жыл бұрын
which theme are you using for VS?
@bobbysugianto5744
@bobbysugianto5744 4 жыл бұрын
Hi, how if I want to cancel the whole wait group when 1 one of the go routine returns an error?
@martint5340
@martint5340 3 жыл бұрын
10:25 how come you didn’t have to pass the pointer to the wait-group (L: 16 created, outside anon function) to the anonymous function within which you perform a method on said waitgroup instance (L: 25)?
@amitsaxena7792
@amitsaxena7792 Жыл бұрын
Good
@iammixaoblivion
@iammixaoblivion 3 жыл бұрын
Why don't you use defer wg.Done() ?
@shleebeez
@shleebeez 4 жыл бұрын
What about when you have wait groups relying on channels too? This is a good example but I'm wondering about putting everything to synchronize. So like, what if you have another function that build urls by reading them from a file (urls being a channel)
@Tutorialedge
@Tutorialedge 4 жыл бұрын
I believe you could extend the goroutines used within this example to simple block execution until they have received a value (your URL I believe) to a certain channel? This way you have a shared channel that you can communicate over and you can control when the function calling the goroutines terminates using the WaitGroups. I would need to see a little more code to understand what is going on here though, but I hope this helps!
@shleebeez
@shleebeez 4 жыл бұрын
@@Tutorialedge yes thank you! i implemented a solution with workers too (like this: golangbot.com/buffered-channels-worker-pools/) . maybe you can do a video on that concept, i believe it is a common pattern i am trying to understand better.
@TroenderTass
@TroenderTass 2 жыл бұрын
This stupid language. Yeah now it has to be a pointer, while if you use channels, then they are references derp derp derp DURRR DURRR. It's like everything in this language is made up on the fly
Understanding Contexts in Go in 5(-ish?) Minutes
15:14
TutorialEdge
Рет қаралды 59 М.
Дибала против вратаря Легенды
00:33
Mr. Oleynik
Рет қаралды 4,6 МЛН
He sees meat everywhere 😄🥩
00:11
AngLova
Рет қаралды 10 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:25
CRAZY GREAPA
Рет қаралды 13 МЛН
Go Goroutine Tutorial - Concurrency in Go
6:23
TutorialEdge
Рет қаралды 34 М.
WaitGroups - Part 1 (Concurrency in Go #2)
17:56
Steve Hook
Рет қаралды 11 М.
Go Mutex Tutorial
8:57
TutorialEdge
Рет қаралды 21 М.
Wait groups in golang
11:54
Hitesh Choudhary
Рет қаралды 23 М.
Working with JSON in Go - Tutorial
14:14
TutorialEdge
Рет қаралды 42 М.
Buffered VS UnBuffered Channels In Golang
11:52
Anthony GG
Рет қаралды 7 М.
DO NOT USE sync.Map in Go!
9:14
rwxrob
Рет қаралды 7 М.
Go Basic Channels Tutorial
7:28
TutorialEdge
Рет қаралды 38 М.
Concurrency and goroutines in golang
13:18
Hitesh Choudhary
Рет қаралды 38 М.
Дибала против вратаря Легенды
00:33
Mr. Oleynik
Рет қаралды 4,6 МЛН