Тёмный

Golang Channels Or Wait Groups? Let Me Explain. 

Anthony GG
Подписаться 66 тыс.
Просмотров 23 тыс.
50% 1

► Join my Discord community for free education 👉 / discord
► Exclusive Lessons, Mentorship, And Videos 👉 / anthonygg_
► Enjoy a 60% Black Friday (limited spots) Discount on My Golang Course 👉 fulltimegodev.com
► Learn how I became a self-taught software engineer 👉fulltimegodev....
► Follow me on Twitter 👉 / anthdm
► Follow me on GitHub 👉 github.com/anthdm
SUBSCRIBE OR NO MARGARITAS
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Опубликовано:

 

27 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 50   
@anthonygg_
@anthonygg_ 10 месяцев назад
► 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 5 месяцев назад
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 Месяц назад
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 10 месяцев назад
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 10 месяцев назад
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
@athinodorossgouromalliis9021
@athinodorossgouromalliis9021 10 месяцев назад
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
@0xastley
@0xastley 10 месяцев назад
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 10 месяцев назад
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.
@kronst
@kronst 10 месяцев назад
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)) }
@saifbenzamit5980
@saifbenzamit5980 10 месяцев назад
G.O.A.T as always ❤
@alexandrk5715
@alexandrk5715 10 месяцев назад
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_ 10 месяцев назад
Ok I will investigate this sounds issue
@CrayonEater9845
@CrayonEater9845 10 месяцев назад
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_ 10 месяцев назад
Yes there are multiple solutions. I just came up with something straightforward and I hope easy to understand for people that are new.
@errorbat
@errorbat 10 месяцев назад
What about the switch{} block and label brakes in for loop instead of using the waitgroups along with channels?
@aamonaze
@aamonaze 10 месяцев назад
legend back
@unpocodetodo6331
@unpocodetodo6331 6 месяцев назад
But at the end if you have more than 2 channels you always use wait groups? I dont understand
@ghoulbond
@ghoulbond 10 месяцев назад
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_ 10 месяцев назад
You are blocking on the for loop so closing wont be triggered
@wMwPlay
@wMwPlay 10 месяцев назад
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_ 10 месяцев назад
100% correct. Context is also a good option. There is a video on that also.
@wMwPlay
@wMwPlay 10 месяцев назад
@@anthonygg_ will check it right nao
@ashishDandgawhale
@ashishDandgawhale 9 месяцев назад
Im wondering why the wg.done() was not called? and without that how did the wg know that all goroutes are comeplted?
@fdefontis
@fdefontis 10 месяцев назад
Great video, both well explained and helpful, thank you!
@HiperCode10100
@HiperCode10100 9 месяцев назад
If I apply to be a partner on the website, are there translation subtitles? thai😢
@worldwide6626
@worldwide6626 10 месяцев назад
2
@it1860
@it1860 10 месяцев назад
1
@takedownccp
@takedownccp 2 месяца назад
You do it wrong, and spear your wrong method to us?
@MightyMoud
@MightyMoud 3 месяца назад
Is there a way to do without a WG when you don't know how many routines will be there?
@hard.nurtai4209
@hard.nurtai4209 12 дней назад
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
@EricT43
@EricT43 9 месяцев назад
From now on I am calling my wait groups "weegee" :D
@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
@mansourbaitar5194
@mansourbaitar5194 10 месяцев назад
YOU ARE HIM
@anthonygg_
@anthonygg_ 10 месяцев назад
😹😹😹
@kangtran3008
@kangtran3008 8 месяцев назад
What's the color theme are you using? Thanks
@Darqonik
@Darqonik 6 месяцев назад
Probably Gruvbox
9 месяцев назад
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
@hebozhe
@hebozhe 3 месяца назад
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.
@rielj
@rielj 7 месяцев назад
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 5 месяцев назад
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")
@jamesblackman1623
@jamesblackman1623 10 месяцев назад
Great - really good simple demo - thanks
@bhavin19
@bhavin19 10 месяцев назад
Hmm, shouldn't it be better if we just defer the Chan closing instead of using sleep?
@amitkumdixit
@amitkumdixit 10 месяцев назад
Why don't we add wg.Done in the local function instead of sleep
@demyk214
@demyk214 10 месяцев назад
Nee maat gewoon assembly
@anthonygg_
@anthonygg_ 10 месяцев назад
🥲
@daltonyon
@daltonyon 5 месяцев назад
Great class!!! Personally I learn a lot
@vanshajdhar9223
@vanshajdhar9223 10 месяцев назад
Best video on channels and waitgroups ❤
@kevinz1991
@kevinz1991 10 месяцев назад
super clear and easy to understand thank u
@shivamkumarz
@shivamkumarz 10 месяцев назад
Please make a video on different rate limiting patterns
@redsnakeintown
@redsnakeintown 10 месяцев назад
Done decrement the number inside the wg.add
Далее
Message Queue From Scratch In Golang
2:29:49
Просмотров 9 тыс.
История Hamster Kombat ⚡️ Hamster Academy
04:14
Beatrise (пародия) Stromae - Alors on danse
00:44
Help Me Celebrate! 😍🙏
00:35
Просмотров 17 млн
Fastest way to become a Software Engineer
9:10
Просмотров 310 тыс.
A Practical Example How To Use Interfaces In Golang
14:42
Microservices are Technical Debt
31:59
Просмотров 304 тыс.
Beginners Should Think Differently When Writing Golang
11:35
Radical Simplicity
45:53
Просмотров 261 тыс.
Async Rust Is A Bad Language | Prime Reacts
28:46
Просмотров 97 тыс.
Buffered VS UnBuffered Channels In Golang
11:52
Просмотров 8 тыс.
История Hamster Kombat ⚡️ Hamster Academy
04:14