In this example we are going to split multiple jobs into set of batch operations where each batch will contain maximum of 3 jobs. Next batch will wait for the previous batch to finish.


Example


package main

import "fmt"

var (
// batchSize defines the batch size where each job batch will contain maximum certain number of jobs.
batchSize = 3
// jobs contains the resources we are going to process.
jobs = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
)

func main() {
fmt.Println("BEGIN")

for i := 0; i < len(jobs); i += batchSize {
j := i + batchSize
if j > len(jobs) {
j = len(jobs)
}

fmt.Printf("batch %d: ", batchNo(i))
processor(jobs[i:j])
}

fmt.Println("END")
}

func processor(jobs []string) {
fmt.Println(jobs)
}

func batchNo(i int) int {
if i == 0 {
i = 1
} else if i == batchSize {
i = 2
} else if i == batchSize * 2 {
i = 3
} else {
i = (i / batchSize) + 1
}

return i
}

Output


BEGIN
batch 1: [a b c]
batch 2: [d e f]
batch 3: [g h i]
batch 4: [j]
END