Golang Channels Or Wait Groups? Let Me Explain.

preview_player
Показать описание


SUBSCRIBE OR NO MARGARITAS
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Рекомендации по теме
Комментарии
Автор

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.

ehSamurai
Автор

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!

ZhaksylykAshim
Автор

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

matiasbpg
Автор

From now on I am calling my wait groups "weegee" :D

EricT
Автор

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

Автор

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.

rielj
Автор

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

athinodorossgouromalliis
Автор

Is there a way to do without a WG when you don't know how many routines will be there?

MightyMoud
Автор

Great - really good simple demo - thanks

jamesblackman
Автор

Best video on channels and waitgroups ❤

vanshajdhar
Автор

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\n", time.Since(start))
}

kronst
Автор

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.

xastley
Автор

Great class!!! Personally I learn a lot

daltonyon
Автор

super clear and easy to understand thank u

kevinz
Автор

What's the color theme are you using?
Thanks

kangtran
Автор

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

CrayonEater
Автор

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.

hebozhe
Автор

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?

ghoulbond
Автор

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

SaiyanJin
Автор

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.

alexandrk