Created
August 20, 2014 00:46
-
-
Save shentonfreude/299c476d5b7e17552de3 to your computer and use it in GitHub Desktop.
Golang channel drunks
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" | |
"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