Skip to content

Instantly share code, notes, and snippets.

@cdemers
Created August 12, 2019 16:14
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 cdemers/cf1580bead21101daf388d3492423e3e to your computer and use it in GitHub Desktop.
Save cdemers/cf1580bead21101daf388d3492423e3e to your computer and use it in GitHub Desktop.
package main
import (
"log"
"sync"
)
func main() {
inChan := make(chan workToDo, 2)
outChan := make(chan workResult, 2)
var wg sync.WaitGroup
wg.Add(1)
go startWorker(inChan, outChan, &wg)
wg.Add(1)
go startWorker(inChan, outChan, &wg)
go func() {
inChan <- workToDo{messageIn: "foo"}
inChan <- workToDo{messageIn: "bar"}
inChan <- workToDo{messageIn: "baz"}
close(inChan)
}()
go func() {
wg.Wait()
close(outChan)
}()
for v := range outChan {
log.Printf("%#v\n", v)
}
}
type workToDo struct {
messageIn string
}
type workResult struct {
messageOut string
}
func startWorker(in chan workToDo, out chan workResult, wg *sync.WaitGroup) {
for v := range in {
log.Printf("%#v\n", v)
out <- workResult{messageOut: "Echo: " + v.messageIn}
}
wg.Done()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment