Skip to content

Instantly share code, notes, and snippets.

@peltho
Created May 3, 2022 11:59
Show Gist options
  • Save peltho/746ecfbb571ce76268d65844d3215caf to your computer and use it in GitHub Desktop.
Save peltho/746ecfbb571ce76268d65844d3215caf to your computer and use it in GitHub Desktop.
Batch processing of channel data
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
sinkChan := make(chan int)
go func() {
sinkChan <- 1
sinkChan <- 2
sinkChan <- 3
sinkChan <- 4
sinkChan <- 5
sinkChan <- 6
sinkChan <- 7
sinkChan <- 8
sinkChan <- 9
}()
var recordCount int = 9
var chunkSize int
if recordCount/3 <= 1 {
chunkSize = 1
} else {
chunkSize = 3
}
fmt.Printf("recordCount: %v | chunkSize: %v\n\n", recordCount, chunkSize)
for i := 0; i < recordCount/chunkSize; i++ {
for i := 0; i < chunkSize; i++ {
wg.Add(1)
go func(i int) {
fmt.Printf("record_%v: %v\n", i, <-sinkChan)
defer wg.Done()
}(i)
}
wg.Wait()
fmt.Println()
}
}
/*
recordCount: 9 | chunkSize: 3
record_1: 3
record_2: 1
record_0: 2
record_2: 4
record_1: 5
record_0: 6
record_2: 7
record_0: 8
record_1: 9
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment