码迷,mamicode.com
首页 > 其他好文 > 详细

non-blocking channel options _ golang

时间:2015-03-17 14:11:05      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

Basic sends and receives on channels are blocking. However, we can use select with a default clause to implement non-blocking sends, receives, and even non-blocking multi-way selects

package main

import (
    "fmt"
)

func main() {

    message := make(chan string)
    signals := make(chan bool)

    select {
    case msg := <-message:
        fmt.Println("received message", msg)
    default:
        fmt.Println("no message received")
    }

    msg := "hi"

    select {
    case message <- msg:
        fmt.Println("sent message", msg)
    default:
        fmt.Println("no message sent")
    }

    select {
    case msg := <-message:
        fmt.Println("received message", msg)
    case sig := <-signals:
        fmt.Println("received signal", sig)
    default:
        fmt.Println("no activity")
    }

}
no message received
no message sent
no activity

总结 :

  1:....

non-blocking channel options _ golang

标签:

原文地址:http://www.cnblogs.com/jackkiexu/p/4344157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!