Skip to content

Instantly share code, notes, and snippets.

@ekusiadadus
Created November 6, 2022 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekusiadadus/d6219e8d12ea5aa9b7f97216f4064750 to your computer and use it in GitHub Desktop.
Save ekusiadadus/d6219e8d12ea5aa9b7f97216f4064750 to your computer and use it in GitHub Desktop.
producer, consumer
package main
import "sync"
func producer(ch chan int, i int) {
ch <- i * 2
}
func consumer(ch chan int, wg *sync.WaitGroup) {
defer wg.Done()
for i := range ch {
println(i)
wg.Done()
}
}
func main() {
var wg sync.WaitGroup
ch := make(chan int)
for i := 0; i < 10; i++ {
wg.Add(1)
go producer(ch, i)
}
go consumer(ch, &wg)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment