Skip to content

Instantly share code, notes, and snippets.

@hsaito
Created March 15, 2012 00:24
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 hsaito/2040677 to your computer and use it in GitHub Desktop.
Save hsaito/2040677 to your computer and use it in GitHub Desktop.
Random test code to figure out concurrency on Go language
package main
import (
"fmt"
"time"
)
func main() {
c1 := make(chan bool)
go func() {
for i := 0; i < 10; i++ {
fmt.Printf(fmt.Sprintf("First: %d\n",i))
time.Sleep(1000000000)
}
c1 <- true;
fmt.Println("First is complete")
} ()
c2 := make(chan bool)
go func() {
for i := 0; i < 10; i++ {
fmt.Printf(fmt.Sprintf("Second: %d\n",i))
time.Sleep(2000000000)
}
c2 <- true;
fmt.Println("Second is complete")
} ()
fmt.Println("Waiting for functions to complete")
<-c1
<-c2
fmt.Println("Done")
}
/*
Output:
Waiting for functions to complete
First: 0
Second: 0
First: 1
Second: 1
First: 2
First: 3
Second: 2
First: 4
First: 5
Second: 3
First: 6
First: 7
Second: 4
First: 8
First: 9
Second: 5
First is complete
Second: 6
Second: 7
Second: 8
Second: 9
Second is complete
Done
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment