Created
October 8, 2023 17:36
-
-
Save fotonmoton/d9b5a50276eb79d8b0422d9176d3c632 to your computer and use it in GitHub Desktop.
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" | |
"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