Created
November 6, 2022 19:52
-
-
Save ekusiadadus/d6219e8d12ea5aa9b7f97216f4064750 to your computer and use it in GitHub Desktop.
producer, consumer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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