Golang Channels Or Wait Groups? Let Me Explain.

  Рет қаралды 15,502

Anthony GG

Anthony GG

6 ай бұрын

► 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.com/#mystory
► Follow me on Twitter 👉 / anthdm
► Follow me on GitHub 👉 github.com/anthdm
SUBSCRIBE OR NO MARGARITAS
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Пікірлер: 44
@anthonygg_
@anthonygg_ 5 ай бұрын
► 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
@ZhaksylykAshim
@ZhaksylykAshim 5 ай бұрын
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 5 ай бұрын
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
@muhammadfahad3483
@muhammadfahad3483 21 күн бұрын
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.
5 ай бұрын
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 5 ай бұрын
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
@ghoulbond
@ghoulbond 5 ай бұрын
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_ 5 ай бұрын
You are blocking on the for loop so closing wont be triggered
@fdefontis
@fdefontis 5 ай бұрын
Great video, both well explained and helpful, thank you!
@troymadsen9845
@troymadsen9845 5 ай бұрын
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_ 5 ай бұрын
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 5 ай бұрын
What about the switch{} block and label brakes in for loop instead of using the waitgroups along with channels?
@jamesblackman1623
@jamesblackman1623 5 ай бұрын
Great - really good simple demo - thanks
@kronst
@kronst 5 ай бұрын
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)) }
@HiperCode10100
@HiperCode10100 4 ай бұрын
If I apply to be a partner on the website, are there translation subtitles? thai😢
@kangtran3008
@kangtran3008 4 ай бұрын
What's the color theme are you using? Thanks
@Darqonik
@Darqonik 2 ай бұрын
Probably Gruvbox
@daltonyon
@daltonyon Ай бұрын
Great class!!! Personally I learn a lot
@wMwPlay
@wMwPlay 5 ай бұрын
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_ 5 ай бұрын
100% correct. Context is also a good option. There is a video on that also.
@wMwPlay
@wMwPlay 5 ай бұрын
@@anthonygg_ will check it right nao
@kevinz1991
@kevinz1991 5 ай бұрын
super clear and easy to understand thank u
@0xastley
@0xastley 5 ай бұрын
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 5 ай бұрын
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.
@unpocodetodo6331
@unpocodetodo6331 Ай бұрын
But at the end if you have more than 2 channels you always use wait groups? I dont understand
@ashishDandgawhale
@ashishDandgawhale 4 ай бұрын
Im wondering why the wg.done() was not called? and without that how did the wg know that all goroutes are comeplted?
@rielj
@rielj 3 ай бұрын
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 25 күн бұрын
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")
@vanshajdhar9223
@vanshajdhar9223 5 ай бұрын
Best video on channels and waitgroups ❤
@bhavin19
@bhavin19 5 ай бұрын
Hmm, shouldn't it be better if we just defer the Chan closing instead of using sleep?
@aamonaze
@aamonaze 5 ай бұрын
legend back
@saifbenzamit5980
@saifbenzamit5980 5 ай бұрын
G.O.A.T as always ❤
@amitkumdixit
@amitkumdixit 5 ай бұрын
Why don't we add wg.Done in the local function instead of sleep
@mansourbaitar5194
@mansourbaitar5194 5 ай бұрын
YOU ARE HIM
@anthonygg_
@anthonygg_ 5 ай бұрын
😹😹😹
@alexandrk5715
@alexandrk5715 5 ай бұрын
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_ 5 ай бұрын
Ok I will investigate this sounds issue
@EricT43
@EricT43 5 ай бұрын
From now on I am calling my wait groups "weegee" :D
@shivamkumarz
@shivamkumarz 5 ай бұрын
Please make a video on different rate limiting patterns
@demyk214
@demyk214 5 ай бұрын
Nee maat gewoon assembly
@anthonygg_
@anthonygg_ 5 ай бұрын
🥲
@worldwide6626
@worldwide6626 5 ай бұрын
2
@it1860
@it1860 5 ай бұрын
1
@redsnakeintown
@redsnakeintown 5 ай бұрын
Done decrement the number inside the wg.add
Message Queue From Scratch In Golang
2:29:49
Anthony GG
Рет қаралды 7 М.
Buffered VS UnBuffered Channels In Golang
11:52
Anthony GG
Рет қаралды 7 М.
Trágico final :(
01:00
Juan De Dios Pantoja
Рет қаралды 25 МЛН
Не пей газировку у мамы в машине
00:28
Даша Боровик
Рет қаралды 9 МЛН
Mini Jelly Cake 🎂
00:50
Mr. Clabik
Рет қаралды 18 МЛН
NO NO NO YES! (50 MLN SUBSCRIBERS CHALLENGE!) #shorts
00:26
PANDA BOI
Рет қаралды 86 МЛН
Advanced Golang: Channels, Context and Interfaces Explained
22:17
Code With Ryan
Рет қаралды 105 М.
Why Frontend development Is More Important Than Backend?
0:53
Anthony GG
Рет қаралды 11 М.
What Is The Definition Of A LOSER?
0:58
Anthony GG
Рет қаралды 1,6 М.
Compiling MS-DOS 4.0 using DOSbox & Qemu
17:59
Neozeed
Рет қаралды 3,3 М.
The Golang Compiler Feature You Might Not Know!?
8:13
Anthony GG
Рет қаралды 10 М.
I'm Starting To Like This Configuration Pattern In Go
11:49
Anthony GG
Рет қаралды 17 М.
How good is the latest version of ChatGPT? | BBC News
23:16
BBC News
Рет қаралды 99 М.
Interview with Senior JS Developer 2024 [NEW]
6:45
Programmers are also human
Рет қаралды 381 М.
What Stack Should You Learn First As Beginner Programmer
0:39
Anthony GG
Рет қаралды 4,7 М.
GPT-4o Deep Dive & Hidden Abilities you should know about
28:11
Trágico final :(
01:00
Juan De Dios Pantoja
Рет қаралды 25 МЛН