Skip to content

Instantly share code, notes, and snippets.

@shentonfreude
Created August 20, 2014 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shentonfreude/299c476d5b7e17552de3 to your computer and use it in GitHub Desktop.
Save shentonfreude/299c476d5b7e17552de3 to your computer and use it in GitHub Desktop.
Golang channel drunks
package main
import (
"fmt"
"strconv"
"time"
)
var i int
func makeDrinker(cs chan string) {
for i := 1; i <= 10; i++ {
name := "Drinker " + strconv.Itoa(i)
fmt.Println(name, " wants a beer")
cs <- name //put drink request on queue (channel)
}
}
func makeBartender(cs chan string) {
for i := 1; i < 100; i++ {
s := <-cs // get what's in the queue
fmt.Println("Serving: ", s)
}
}
func main() {
cs := make(chan string)
go makeDrinker(cs)
go makeBartender(cs)
time.Sleep(10 * 1e9)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment