Skip to content

Instantly share code, notes, and snippets.

@kjk
Created November 5, 2019 09:10
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 kjk/2f155938fdd0d264618bcd8b38b091b6 to your computer and use it in GitHub Desktop.
Save kjk/2f155938fdd0d264618bcd8b38b091b6 to your computer and use it in GitHub Desktop.
// :collection Essential Go
package main
import (
"fmt"
"sync"
)
// :show start
var wg sync.WaitGroup
func sqrtWorker(chIn chan int, chOut chan int) {
fmt.Printf("sqrtWorker started\n")
for i := range chIn {
sqrt := i * i
chOut <- sqrt
}
fmt.Printf("sqrtWorker finished\n")
wg.Done()
}
func main() {
chIn := make(chan int)
chOut := make(chan int)
for i := 0; i < 2; i++ {
wg.Add(1)
go sqrtWorker(chIn, chOut)
}
go func() {
chIn <- 2
chIn <- 4
close(chIn)
}()
go func() {
wg.Wait()
close(chOut)
}()
for sqrt := range chOut {
fmt.Printf("Got sqrt: %d\n", sqrt)
}
}
// :show end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment