Skip to content

Instantly share code, notes, and snippets.

@stantonk
Created March 12, 2014 04:23
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 stantonk/9500807 to your computer and use it in GitHub Desktop.
Save stantonk/9500807 to your computer and use it in GitHub Desktop.
Messing with channels and goroutines
package main
import "fmt"
import "time"
func consume(c chan int) {
for {
fmt.Println("sleeping for a second...")
time.Sleep(1000 * time.Millisecond)
fmt.Println("consumed:", <-c)
}
}
func main() {
c := make(chan int, 2)
go consume(c)
c <- 1
c <- 2
fmt.Println("now 2 elements are in the buffer..")
c <- 3
fmt.Println("if advances to here, spot in buffer opened up for 3")
c <- 4
fmt.Println("if advances to here, spot in buffer opened up for 4")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment