您的位置:首页 > 编程语言 > Go语言

golang channel basic

2015-10-16 10:09 441 查看
package mainimport (    "fmt"    "math/rand"    "time")func main() {    rand.Seed(time.Now().UnixNano())    q := make(chan string)    words := []string{"Enjoy", "Go", "Coding", "Patrick"}    t := time.Now()    for _, w := range words {        // passing w to each goroutine to avoid repeating the same! Try not to.        go func(w string) {            time.Sleep(time.Duration(rand.Int63n(1e9)))            q <- w        }(w)    }    for i := 0; i < len(words); i++ {        // reading from channel q        fmt.Printf("%q\t\t", <-q)        fmt.Println("Created in:", time.Now().Sub(t))    }}

sleep 范围是0 - 1e9(1000000000)

+5level:2exp:5/50
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: