Created
August 12, 2019 16:14
-
-
Save cdemers/cf1580bead21101daf388d3492423e3e to your computer and use it in GitHub Desktop.
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 ( | |
"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