Skip to content

Instantly share code, notes, and snippets.

@Tonkpils
Created June 11, 2015 00:33
Show Gist options
  • Save Tonkpils/bcc5e9d5243b52d25ceb to your computer and use it in GitHub Desktop.
Save Tonkpils/bcc5e9d5243b52d25ceb to your computer and use it in GitHub Desktop.
Simple Goroutines examples
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)
}
$ 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