Skip to content

Instantly share code, notes, and snippets.

@havlan
Last active August 16, 2021 20:33
Show Gist options
  • Select an option

  • Save havlan/b3952d94984f8ad871cb61065acdb66e to your computer and use it in GitHub Desktop.

Select an option

Save havlan/b3952d94984f8ad871cb61065acdb66e to your computer and use it in GitHub Desktop.
Go pipeline concurrency pattern using channels.
package main
import (
"fmt"
"math/rand"
"strings"
)
const (
Alphabet string = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
)
/*
Pipeline pattern, useful when transforming data all the way from database all the way to the client
1. Generate/retrieve data
2. Transform data
3. Return data
*/
func Pipeline() {
recordsChannel := GenerateRecords()
transformedRecords := TransformRecords(recordsChannel)
for record := range transformedRecords {
fmt.Println(record)
}
}
func GenerateRecords() <-chan string {
out := make(chan string)
go func() {
for k := 0; k < 20; k++ {
data := strings.Builder{}
for i := 0; i < 10; i++ {
data.WriteString(string(Alphabet[rand.Intn(len(Alphabet))]))
}
out <- data.String()
}
close(out)
}()
return out
}
func TransformRecords(recordChannel <-chan string) <-chan string {
out := make(chan string)
go func() {
// receive and transform value
for rec := range recordChannel {
out <- strings.ToUpper(rec)
}
// close out channel
close(out)
}()
return out
}
func main() {
Pipeline()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment