Skip to content

Instantly share code, notes, and snippets.

@jahfer
Last active December 15, 2015 00:59
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 jahfer/5176870 to your computer and use it in GitHub Desktop.
Save jahfer/5176870 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func main() {
// set up our in/out pipe
nums := make(chan int, 3)
// anonymous functions!
// the 'go' keyword will run this function in a new thread
go func() {
// lets pause for a millisecond to demo concurrency
time.Sleep(1)
nums <- 1
}()
go func() {
nums <- 2
}()
go func() {
nums <- 3
}()
// <-var will block until a value is received
x := <-nums
y := <-nums
z := <-nums
// Result depends on which functions finish first
fmt.Printf("%d + %d + %d = %d\n", x, y, z, x+y+z)
// Usually "2 + 3 + 1 = 6"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment