Skip to content

Instantly share code, notes, and snippets.

@manigandand
Created January 28, 2020 06:28
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 manigandand/5eec433bdb17624156cb4dd53a9c5bc6 to your computer and use it in GitHub Desktop.
Save manigandand/5eec433bdb17624156cb4dd53a9c5bc6 to your computer and use it in GitHub Desktop.
simple buffered concurrency golang
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