Last active
August 16, 2021 20:33
-
-
Save havlan/b3952d94984f8ad871cb61065acdb66e to your computer and use it in GitHub Desktop.
Go pipeline concurrency pattern using channels.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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