Skip to content

Instantly share code, notes, and snippets.

@jchv
Created June 3, 2015 19:45
Show Gist options
  • Save jchv/f95b723d6af93afd6806 to your computer and use it in GitHub Desktop.
Save jchv/f95b723d6af93afd6806 to your computer and use it in GitHub Desktop.
Playing with goroutines
package main
import "fmt"
import "time"
func main() {
c := make(chan string, 3)
go func() {
c <- "Testing, "
}()
go func() {
c <- "1... "
}()
go func() {
c <- "2... "
}()
go func() {
fmt.Println("In last callback.")
time.Sleep(time.Second * 1)
c <- "3! "
}()
fmt.Println("Executing loop...")
for i := 0; i < 4; i++ {
select {
case msg := <-c:
fmt.Println(msg)
}
}
}
% ./hello
Executing loop.
In last callback.
Testing,
1...
2...
3!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment