Golang Channels Or Wait Groups? Let Me Explain.

  Рет қаралды 26,165

Anthony GG

Anthony GG

Күн бұрын

Пікірлер: 54
@anthonygg_
@anthonygg_ Жыл бұрын
► Join my Discord community for free education 👉 discord.com/invite/Ac7CWREe58 ► Exclusive Lessons, Mentorship, And Videos 👉 www.patreon.com/anthonygg_ ► 60% BLACK FRIDAY DISCOUNT on my Golang course 👉 fulltimegodev.com Thanks for watching
@ehSamurai3483
@ehSamurai3483 7 ай бұрын
TL;DR Use waitgroups when you don't want goroutine to return some data. Use channels when you do want goroutine to return some data.
@dukim632
@dukim632 3 ай бұрын
you can build your own waitGroup with go routine and channels, its just an abstraction you can either use it if it perfectly fits your needs or build one that does
@ZhaksylykAshim
@ZhaksylykAshim Жыл бұрын
pls make a video about fan-in, fan-out patterns and usage of select keyword when working with multiple channels (also would be great if buffers, read and write chanells would be also discussed). My appreciation, maestro!
@matiasbpg
@matiasbpg Жыл бұрын
I think you could skip the wg in the last example by using just an int that is decremented inside the for loop and making the loop sync inside the main. When the last value is received and the counter is zero, just close the channel and the execution of the main function continues
@rielj
@rielj 9 ай бұрын
I think the reason why it didn't execute the last print is that the block that executes the channels' print is in a separate go routine, and the main function is also a go routine meaning that it's not included on the WaitGroup, such that once the WaitGroup is done on the main go routine it already exited the function hence the print inside the go routine is not included on the WaitGroup and is executed after the main go routine has done its task.
@krakhuba
@krakhuba 7 ай бұрын
I've solved the problem in video by creation of second Waitgroup for processing results and closing channel after all workers are done - e.g. after wg.Wait() exists, now it works flawlessly (noob in Go, so I believe there are something like simple mutex instead of Workgroup for awaiting results processed in Go, but I know nothing yet about it): wgProcess := sync.WaitGroup{} wgProcess.Add(1) wg = &sync.WaitGroup{} wg.Add(3) go dowork(2*time.Second, ch) go dowork(4*time.Second, ch) go dowork(6*time.Second, ch) go func() { for r := range ch { fmt.Println(r) } fmt.Printf("Work done in %v ", time.Since(start)) wgProcess.Done() }() wg.Wait() close(ch) wgProcess.Wait() fmt.Println("Exited")
@CodingOffRails
@CodingOffRails 12 күн бұрын
That's what I was thinking too, I agree
11 ай бұрын
Amazing video! I learned to add the waiter and the close into an anonymous go routine and keep the code out of the anonymous go routine. Thanks for this rich material
@athinodorossgouromalliis9021
@athinodorossgouromalliis9021 Жыл бұрын
GG video man. When I was learning golang the when to use go routines with channels was a bit hard to understand and I wish your video was out back then! Cheers mate good job
@EricT43
@EricT43 11 ай бұрын
From now on I am calling my wait groups "weegee" :D
@MightyMoud
@MightyMoud 5 ай бұрын
Is there a way to do without a WG when you don't know how many routines will be there?
@hard.nurtai4209
@hard.nurtai4209 2 ай бұрын
well you always know. you don’t have to add to waitgroup before everything. you can add to it when you about to spawn another goroutine
@jamesblackman1623
@jamesblackman1623 Жыл бұрын
Great - really good simple demo - thanks
@kronst
@kronst Жыл бұрын
Are there any disadvantages of using this approach? For example if we need to make multiple parallel requests to fetch different type of results. func main() { start := time.Now() var r1 string var r2 int var r3 time.Time wg := sync.WaitGroup{} wg.Add(3) go func(wg *sync.WaitGroup) { r1 = "Hello" time.Sleep(1 * time.Second) wg.Done() }(&wg) go func(wg *sync.WaitGroup) { r2 = 42 time.Sleep(2 * time.Second) wg.Done() }(&wg) go func(wg *sync.WaitGroup) { r3 = time.Now() time.Sleep(3 * time.Second) wg.Done() }(&wg) wg.Wait() fmt.Println(r1, r2, r3) fmt.Printf("took: %v ", time.Since(start)) }
@vanshajdhar9223
@vanshajdhar9223 Жыл бұрын
Best video on channels and waitgroups ❤
@0xastley
@0xastley Жыл бұрын
When you demonstrated channels, I think you can make(chan string, size) so you can mimick the config you had with waitgroups. You'll only be expecting 3 messages into the channel and then close it.
@matiasbpg
@matiasbpg Жыл бұрын
You are describing a buffered channel, which is really different from a channel which your only expect 3 values. What the buffer does is to prevent the channel from blocking when giving it values up to the capacity.
@hebozhe
@hebozhe 5 ай бұрын
I don't use channels unless something is asynchronous forever. I just sense I'm wasting code harvesting channel results, when mutexes allow for arbitrarily large expansions into accepted containers.
@kevinz1991
@kevinz1991 Жыл бұрын
super clear and easy to understand thank u
@bhavin19
@bhavin19 Жыл бұрын
Hmm, shouldn't it be better if we just defer the Chan closing instead of using sleep?
@kangtran3008
@kangtran3008 10 ай бұрын
What's the color theme are you using? Thanks
@Darqonik
@Darqonik 8 ай бұрын
Probably Gruvbox
@daltonyon
@daltonyon 7 ай бұрын
Great class!!! Personally I learn a lot
@SaiyanJin85
@SaiyanJin85 2 ай бұрын
but we need the the result inside the main, adding the the for loop inside a goroutine then we can't have the results in the main
@aamonaze
@aamonaze Жыл бұрын
legend back
@saifbenzamit5980
@saifbenzamit5980 Жыл бұрын
G.O.A.T as always ❤
@errorbat
@errorbat Жыл бұрын
What about the switch{} block and label brakes in for loop instead of using the waitgroups along with channels?
@MrWesopl
@MrWesopl 12 сағат бұрын
Is it really the compiler being smart or the runtime? Inhoni think has nothing to do with compiler if deadlocks detected. Anyway, I love your videos it's easy to understand. 🤷‍♂️
@CrayonEater9845
@CrayonEater9845 Жыл бұрын
Would it make sense to invert your example, pass the wg.Wait to your anonymous function and have the channel for on the main thread to get results back to main thread? I suppose you could also pass a slice to the function instead of the wg
@anthonygg_
@anthonygg_ Жыл бұрын
Yes there are multiple solutions. I just came up with something straightforward and I hope easy to understand for people that are new.
@ghoulbond
@ghoulbond Жыл бұрын
Thanks Anthony, just a quick question is it a possible to close the channel inside the go routine in this way go func() { for res := range resultch { fmt.Println(res) } fmt.println() close(resultch) }() if it is possible, is it considered a good practice?
@anthonygg_
@anthonygg_ Жыл бұрын
You are blocking on the for loop so closing wont be triggered
@amitkumdixit
@amitkumdixit Жыл бұрын
Why don't we add wg.Done in the local function instead of sleep
@alexandrk5715
@alexandrk5715 Жыл бұрын
Volume becomes lower and lower with every next video, please make it at least a little bit louder, it's becoming hard to listen. Thanks for your work.
@anthonygg_
@anthonygg_ Жыл бұрын
Ok I will investigate this sounds issue
@guinea_horn
@guinea_horn Ай бұрын
Audio is super low, had to max out my volume to hear
@luksmrz
@luksmrz 8 ай бұрын
But at the end if you have more than 2 channels you always use wait groups? I dont understand
@mansourbaitar5194
@mansourbaitar5194 Жыл бұрын
YOU ARE HIM
@anthonygg_
@anthonygg_ Жыл бұрын
😹😹😹
@wMwPlay
@wMwPlay Жыл бұрын
But it feels like using both channels with waitgroups are taking more resources. Didn't test myself but heard using channels is much efficient and preffered than wg. Any other approaches to control that job is done? contexts?
@anthonygg_
@anthonygg_ Жыл бұрын
100% correct. Context is also a good option. There is a video on that also.
@wMwPlay
@wMwPlay Жыл бұрын
@@anthonygg_ will check it right nao
@ashishDandgawhale
@ashishDandgawhale 11 ай бұрын
Im wondering why the wg.done() was not called? and without that how did the wg know that all goroutes are comeplted?
@fdefontis
@fdefontis Жыл бұрын
Great video, both well explained and helpful, thank you!
@habibaabueldahab6287
@habibaabueldahab6287 Ай бұрын
WEEEGEEE >>>>>>
@shivamkumarz
@shivamkumarz Жыл бұрын
Please make a video on different rate limiting patterns
@HiperCode10100
@HiperCode10100 11 ай бұрын
If I apply to be a partner on the website, are there translation subtitles? thai😢
@worldwide6626
@worldwide6626 Жыл бұрын
2
@demyk214
@demyk214 Жыл бұрын
Nee maat gewoon assembly
@anthonygg_
@anthonygg_ Жыл бұрын
🥲
@it1860
@it1860 Жыл бұрын
1
@redsnakeintown
@redsnakeintown Жыл бұрын
Done decrement the number inside the wg.add
Message Queue From Scratch In Golang
2:29:49
Anthony GG
Рет қаралды 9 М.
Why Didn't He Get the Job? Let's Find Out! // Code Review
27:25
The Cherno
Рет қаралды 149 М.
Мама у нас строгая
00:20
VAVAN
Рет қаралды 10 МЛН
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 47 МЛН
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 4,1 МЛН
How To Use Goroutines For Aggregating Data In Golang?!
17:15
Anthony GG
Рет қаралды 42 М.
This Will Make Everyone Understand Golang Interfaces
21:03
Anthony GG
Рет қаралды 55 М.
This is why Go Channels are awesome
6:06
Web Dev Cody
Рет қаралды 15 М.
CodeWars: Sum Strings as Numbers
16:03
James Kimbell
Рет қаралды 20
Go Has Exceptions??
16:58
ThePrimeTime
Рет қаралды 85 М.
A Beautiful Way To Deal With ERRORS in Golang HTTP Handlers
8:42
Why I'm learning Go
21:35
Web Dev Cody
Рет қаралды 69 М.
Golang Context Package: It's More Than You Think!
8:26
Alex Mux
Рет қаралды 1,7 М.
The Power Of Struct Embedding And Interfaces In Golang
15:05
Anthony GG
Рет қаралды 16 М.
Мама у нас строгая
00:20
VAVAN
Рет қаралды 10 МЛН