Skip to content

Instantly share code, notes, and snippets.

@kyl0b1te
Created February 27, 2018 20:17
Show Gist options
  • Save kyl0b1te/dbd7ff58def2827a0bdbdf4df079882d to your computer and use it in GitHub Desktop.
Save kyl0b1te/dbd7ff58def2827a0bdbdf4df079882d to your computer and use it in GitHub Desktop.
Golang producer/consumer interaction
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