Created
January 28, 2020 06:28
-
-
Save manigandand/5eec433bdb17624156cb4dd53a9c5bc6 to your computer and use it in GitHub Desktop.
simple buffered concurrency golang
This file contains hidden or 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 ( | |
"fmt" | |
"sync" | |
) | |
func main() { | |
input := []int{1, 2, 3, 4, 5} | |
in := make(chan int) | |
var wg sync.WaitGroup | |
wg.Add(2) | |
go write(input, in, &wg) | |
go read(in, &wg) | |
wg.Wait() | |
fmt.Println("Done..") | |
} | |
func write(input []int, in chan int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
for _, i := range input { | |
in <- i | |
} | |
close(in) | |
return | |
} | |
func read(in chan int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
for out := range in { | |
fmt.Println(out) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment