From 757b29faa40bc7c0882771e5d46e6191d83d5712 Mon Sep 17 00:00:00 2001 From: Kicool Date: Tue, 13 Mar 2012 10:25:27 +0800 Subject: [PATCH] add sample goroutine --- goroutine/gort1.go | 40 ++++++++++++++++++++++++++++++++++++++++ goroutine/gort3.go | 17 +++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 goroutine/gort1.go create mode 100644 goroutine/gort3.go diff --git a/goroutine/gort1.go b/goroutine/gort1.go new file mode 100644 index 0000000..404c3ef --- /dev/null +++ b/goroutine/gort1.go @@ -0,0 +1,40 @@ +package main + +import ( + "time" +) + +func server(i int) { + for { + print(i) + time.Sleep(1*1e9) + } +} + +/* +* this will no output if default: export GOMAXPROCS=1 + */ +func f1() { + go server(1) + go server(2) + for {} +} + +func f3() { + go server(1) + go server(2) + for { + time.Sleep(1*1e6) + } +} + +func f2() { + go server(1) + go server(2) + time.Sleep(6*1000*1000) +} + +func main() { + f3() + //f2() +} diff --git a/goroutine/gort3.go b/goroutine/gort3.go new file mode 100644 index 0000000..44a4255 --- /dev/null +++ b/goroutine/gort3.go @@ -0,0 +1,17 @@ +package main + +import ( + "time" + "fmt" +) + +func IsReady(what string, minutes int64) { + time.Sleep(minutes * 60 * 1e9) // Unit is nanosecs. + fmt.Println(what, "is ready") +} + +func main() { + go IsReady("tea", 6) + go IsReady("coffee", 2) + fmt.Println("I'm waiting...") +}