Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BatchChannel example
package main
import (
"fmt"
"strings"
)
var batchSize int = 2
func main() {
inputChannel := make(chan string, 10)
batchedChannel := make(chan []string)
quit := make(chan bool)
go func() {
for {
next, more := <-batchedChannel
fmt.Println(strings.Join(next, " "))
if !more {
quit <- true
return
}
}
}()
go func() {
for i := 1; i <= 10; i++ {
inputChannel <- fmt.Sprintf("%d", i)
}
close(inputChannel)
}()
go BatchChannel(inputChannel, batchedChannel, batchSize)
<-quit
}
func BatchChannel(channel chan string, batchedChannel chan []string, batchSize int) {
buff := make([]string, 0, batchSize)
for {
next, more := <-channel
if more {
buff = append(buff, next)
if len(buff) >= batchSize {
batchedChannel <- buff
buff = make([]string, 0, batchSize)
}
} else {
if len(buff) > 0 {
batchedChannel <- buff
}
close(batchedChannel)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.