Created
February 27, 2018 20:17
-
-
Save kyl0b1te/dbd7ff58def2827a0bdbdf4df079882d to your computer and use it in GitHub Desktop.
Golang producer/consumer interaction
This file contains 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() { | |
list := make(chan int) | |
go consumer(list) | |
producer(list) | |
} | |
func producer(ch chan int) { | |
i := 1 | |
for { | |
time.Sleep(time.Second * 2) | |
ch <- i | |
i++ | |
} | |
} | |
func consumer(ch chan int) { | |
var message int | |
for { | |
select { | |
case message = <-ch: | |
go say(message) | |
} | |
} | |
} | |
func say(message int) { | |
fmt.Printf("say: %d\n", message) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment