Skip to content

Instantly share code, notes, and snippets.

@cloudaice
Created August 1, 2013 13:48
Show Gist options
  • Save cloudaice/6131509 to your computer and use it in GitHub Desktop.
Save cloudaice/6131509 to your computer and use it in GitHub Desktop.
Go 时间驱动 协程库
package main
import "fmt"
import "time"
import "runtime"
func main(){
runtime.GOMAXPROCS(8)
channel := make(chan string)
for i:=0; i<5; i++ {
go func (id int) {
for {
time.Sleep(3)
msg := <-channel
fmt.Println(msg, id)
}
}(i)
}
ticker := time.NewTicker(time.Second)
for t := range ticker.C {
fmt.Println(t)
channel <- "hello world"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment