Created
March 12, 2014 04:23
-
-
Save stantonk/9500807 to your computer and use it in GitHub Desktop.
Messing with channels and goroutines
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" | |
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