With reference to this example, i want to add a pre-defined delay between job initialization and termination. I have stored the data i.e. jobid and waittime in map. Then i have copied the entire map in a channel of same struct type as map. But i am not able to fetch the map values in go routine calls. Please help me, I am new to Go.
package main
import "fmt"
type Vertex struct {
id, waitime int
}
var m = map[int]Vertex{
1: {1, 1000},
2: {2, 2000},
3: {3, 1000},
4: {4, 2000},
5: {5, 1000},
6: {6, 2000},
7: {7, 1000},
8: {8, 2000},
9: {9, 1000},
10: {10, 2000},
}
func worker(w int, jobs <-chan Vertex, results chan<- int) {
for j := 1; j <= len(m); j++ {
a, b := <-jobs.id, <-jobs.waitime
fmt.Println("worker", w, "started job", a)
//time.Sleep(time.Sleep(time.Duration(b)))
fmt.Println("worker", w, "finished job", a)
results <- j * 2
}
}
func main() {
//n := 5
jobs := make(chan Vertex, 100)
results := make(chan int, 100)
for w := 1; w <= 5; w++ {
go worker(w, jobs, results)
}
fmt.Println(len(m))
for j := 1; j <= len(m); j++ {
jobs <- m[j]
}
//close(jobs)
for a := 1; a <= len(m); a++ {
<-results
}
}