Created
May 21, 2020 14:05
-
-
Save RicardoLinck/79eaf3f8056c0f2d24e97ee748d79d22 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" | |
"time" | |
) | |
func main() { | |
c := make(chan int, 3) | |
q := 10 | |
go worker(c, 1) | |
go worker(c, 2) | |
go worker(c, 3) | |
go generateInts(c, q) | |
time.Sleep(10 * time.Second) | |
} | |
func worker(c chan int, id int) { | |
for i := range c { | |
fmt.Printf("Worker %d received value %d\n", id, i) | |
time.Sleep(1 * time.Second) | |
} | |
} | |
func generateInts(c chan int, q int) { | |
for i := 0; i < q; i++ { | |
c <- i | |
fmt.Printf("Sent value %d\n", i) | |
time.Sleep(1 * time.Millisecond) | |
} | |
close(c) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment