Skip to content

Instantly share code, notes, and snippets.

@angeldm
Created April 27, 2012 16:33
Show Gist options
  • Save angeldm/2510620 to your computer and use it in GitHub Desktop.
Save angeldm/2510620 to your computer and use it in GitHub Desktop.
Worker
package main
import (
"fmt"
"time"
)
var work = 5
type Request struct {
fn func() int
c chan int
}
// Simulation of some work: just sleep for a while and report how long.
func op() int {
n := 5 * time.Second
time.Sleep(time.Duration(n))
fmt.Println("Done.")
return int(n)
}
func do(c,e chan int) {
n := op()
c <- n
work--
}
func doWork() {
c := make(chan int, 10)
e := make(chan int)
for i := 0; i < work; i++ {
go do(c,e)
//op()
}
//work=0
for work!=0 {
select {
case <-c :
fmt.Println(work)
case <-e:
fmt.Println("Finish")
}
}
}
func main() {
fmt.Println("Go !")
doWork()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment