咖啡伴侣

呆在上海
posts - 163, comments - 156, trackbacks - 0, articles - 2

golang chan 超时

Posted on 2013-12-24 13:03 oathleo 阅读(7260) 评论(0)  编辑  收藏
package main

import (
    "fmt"
    "time"
)

var ch chan int = make(chan int, 1)

func main() {
    go aaa()

    select {
    case <-ch: //拿到锁
        fmt.Println("call")
    case <-time.After(5 * time.Second): //超时5s
        fmt.Println("5 sec call")
    }
}

func aaa() {
    time.Sleep(time.Second * 3)
    ch <- 1
}

只有注册用户登录后才能发表评论。


网站导航: