go channel例子
package main import "fmt"
import "time" func main() {
c := make(chan int) //初始化一个管道
defer close(c) //在main函数执行完毕之后执行。
go func() { //会开启一个协程,并往管道c写入数据
time.Sleep( * time.Second)
fmt.Println("all ready")
c <- +
}()
i := <-c // 将管道c的内容输出赋值到i,在c还没有内容的时候,会一直阻塞在这里。
fmt.Println(i) //打印i的值
} [root@localhost hello]# go run channel.go
all ready
package main import "fmt"
import "time" func main() {
c := make(chan int) //初始化一个管道
go func() { //会开启一个协程,并往管道c写入数据
time.Sleep( * time.Second)
fmt.Println("all ready")
c <- +
close(c)
}()
i := <-c // 将管道c的内容输出赋值到i,在c还没有内容的时候,会一直阻塞在这里。
fmt.Println(i) //打印i的值
c <- //由于c已经在协程里面被关闭,这句将引起run-time panic }
输出结果如下:
[root@localhost hello]# go run channel.go
all ready panic: send on closed channel goroutine [running]:
main.main()
/mnt/hgfs/share/eclipse/testgo/src/hello/channel.go: +0x125
exit status
func main() {
go func() {
time.Sleep( * time.Hour)
}()
c := make(chan int)
go func() {
for i := ; i < ; i = i + {
c <- i
}
close(c) //如果将此句注释掉,那么下面的for range在打印完管道的内容后会一直阻塞。
}()
for i := range c {
fmt.Println(i)
}
fmt.Println("Finished")
}
package main import (
"fmt"
) func fab(c, quit chan int) {
x, y := , for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
return
}
} } func main() {
c := make(chan int)
quit := make(chan int) go func() {
for i := ; i < ; i++ {
fmt.Println(<-c)
}
quit <-
}() fab(c, quit) } [root@localhost hello]# go run channel.go
package main import (
"fmt"
"time"
) func main() { c := make(chan string) go func() {
time.Sleep( * time.Second)
c <- "Hello World"
}() select {
case res := <-c:
fmt.Println(res)
case <-time.After( * time.Second):
fmt.Println("timeout")
} }
[root@localhost hello]# go run channel.go
timeout
参考来源:http://colobu.com/2016/04/14/Golang-Channels/
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
go channel例子的更多相关文章
- 如何使用 channel
如何使用 Channel 例子来自于Concurrency is not parallelism Google Search: A fake framework v1.0 var ( Web = fa ...
- Asterisk manager API(AMI)文档(中文版)
Asterisk控制接口(AMI)允许管理客户端程序连接到一个asterisk实例并且可以通过TCP/IP流发送命令或读取事件.这在试图跟踪asterisk的状态或其中的电话客户端状态时很有用,AMI ...
- asterisk manager api 配置 (manager.conf)
http://blog.csdn.net/niino/article/details/5748805 要激活AMI,需要在/etc/asterisk/manager.conf中,[general]块下 ...
- Goroutines和Channels
原文链接 https://golangbot.com/goroutines/ Goroutines Goroutines 可以被认为是多个函数或方法同时允许.可以认为是一个轻量级的线程.与线程的花费相 ...
- 一个Golang例子:for + goroutine + channel
Rob Pike 在 Google I/O 2012 - Go Concurrency Patterns 里演示了一个例子(daisy chain). 视频地址:https://www.youtube ...
- golang 部分理解:关于channel 和 goroutine 例子
部分理解:关于channel 和 goroutine 例子package main import "strconv" import "fmt" func mai ...
- Golang, 以17个简短代码片段,切底弄懂 channel 基础
(原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...
- TODO:Go语言goroutine和channel使用
TODO:Go语言goroutine和channel使用 goroutine是Go语言中的轻量级线程实现,由Go语言运行时(runtime)管理.使用的时候在函数前面加"go"这个 ...
- channel Golang
Golang, 以17个简短代码片段,切底弄懂 channel 基础 (原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的 ...
随机推荐
- python标准库介绍——33 thread 模块详解
?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...
- 将多个 docx 文件使用 POI 进行合并,生成单个文档,包含图片
1 添加 maven 依赖,需要使用 poi 的依赖项 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad ...
- PHP实现无符号右移(js中的 >>>)
移位包括有符号左移(<<).有符号右移(>>).无符号右移(>>>),其中 js 支持三种移位,PHP只支持前两种移位(没查到第三种),恰好需要PHP进行无符 ...
- SpringBoot actuator 应用监控。
前言 : 今天在阅读 <SpringCloud微服务实战>一书时看到了SpringBoot actuator相关知识,并且自己也本地调试实践.觉得SpringBoot这一套监控还是挺有意思 ...
- OpenXml SDK 2.0 创建Word文档 添加页、段落、页眉和页脚
using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(@"C:\********.doc ...
- elk 使用中遇到的问题(kafka 重复消费)
问题描述: 在使用过程中,当遇到大量报错的时候,我们到eagle后台看到报错的那个consumer的消费情况到到lag 远远大于0(正常情况应该为0),activie 节点没有,kibana面板上没 ...
- grafana + influxdb + telegraf
grafana + influxdb + telegraf , 构建性能监控平台http://www.cnblogs.com/Scissors/p/5977670.html https://docs. ...
- Android Studio 通过 git update 或者 pull 的时候出错及解决办法
Android Studio 通过 git update 或者 pull 的时候出错,log 如下: Couldn't save uncommitted changes. Tried to save ...
- 每日英语:China's Wistful Wen Gets His Wish
As his term as premier was drawing to a close, Wen Jiabao reflected wistfully on some of the goals h ...
- 【Linux】了解服务器的情况
Java程序大多数都部署在Unix环境,而环境的稳定性对于部署的应用至关重要,所以Java开发人员需知道了解Unix环境的命令. 系统版本 查看系统版本 [root@localhost third_p ...