Created
June 11, 2015 00:33
-
-
Save Tonkpils/bcc5e9d5243b52d25ceb to your computer and use it in GitHub Desktop.
Simple Goroutines examples
This file contains hidden or 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" | |
"time" | |
) | |
func longFunction(name string, dur time.Duration) { | |
fmt.Printf("Started %s: lasting %s\n", name, dur) | |
time.Sleep(dur) | |
fmt.Printf("Finished %s\n", name) | |
} | |
func main() { | |
go longFunction("long event", time.Duration(10*time.Second)) | |
go longFunction("medium event", time.Duration(6*time.Second)) | |
go longFunction("short event", time.Duration(3*time.Second)) | |
time.Sleep(12 * time.Second) | |
} |
This file contains hidden or 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
$ go run goroutines.go | |
Started long event: lasting 10s | |
Started medium event: lasting 6s | |
Started short event: lasting 3s | |
Finished short event | |
Finished medium event | |
Finished long event |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment