Skip to content

Instantly share code, notes, and snippets.

@fotonmoton
Created October 8, 2023 17:36
Show Gist options
  • Save fotonmoton/d9b5a50276eb79d8b0422d9176d3c632 to your computer and use it in GitHub Desktop.
Save fotonmoton/d9b5a50276eb79d8b0422d9176d3c632 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"time"
)
type image int
type imageQueue chan image
func bigImageGenerator() imageQueue {
queue := make(imageQueue)
go func() {
for i := 0; i < 1000; i++ {
img := rand.Intn(100)
fmt.Println("new big image:", img)
queue <- image(img)
}
close(queue)
}()
return queue
}
func compressImage(bigImages imageQueue, compressed imageQueue) {
for {
select {
case image := <-bigImages:
fmt.Println("compressing big image:", image)
image = image / 10
time.Sleep(time.Second * 2 * time.Duration(rand.Intn(10)))
compressed <- image
}
}
}
func main() {
bigImages := bigImageGenerator()
compressedImages := make(imageQueue)
for range make([]int, 10) {
go compressImage(bigImages, compressedImages)
}
for compressedImage := range compressedImages {
fmt.Println("Compressed:", compressedImage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment