Skip to content

Latest commit

 

History

History
59 lines (46 loc) · 876 Bytes

job-channel.md

File metadata and controls

59 lines (46 loc) · 876 Bytes

Job Channel

Create a struct with result channel and payload, and pass them to another channel to be processed. Upon completion, the result is send back to the result channel.

package main

import (
	"fmt"
	"sync"
	"time"
)

type Job struct {
	resultCh chan string
	payload  string
}

func main() {

	worker := make(chan Job)
	numWorkers := 3
	for i := 0; i < numWorkers; i++ {
		go func() {
			for job := range worker {
				time.Sleep(1 * time.Second)
				job.resultCh <-job.payload + " completed"
			}
		}()
	}

	n := 10

	var wg sync.WaitGroup
	wg.Add(n)

	doWork := func(i int) {
		defer wg.Done()

		job := Job{
			resultCh: make(chan string), 
			payload: fmt.Sprintf("job#%d", i),
		}

		worker <- job
		msg := <-job.resultCh
		fmt.Println(msg)
	}

	for i := 0; i < n; i++ {
		go func(i int) {
			doWork(i)
		}(i)
	}

	wg.Wait()
	fmt.Println("exiting")
}