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的 ...
随机推荐
- maven pom文件简单模板和配置详解
https://blog.csdn.net/earbao/article/details/49924943 maven pom文件简单模板和配置详解
- IntelliJ IDEA设置代码括号对齐方式
IntelliJ IDEA设置代码括号对齐方式 IntelliJ IDEA默认的对齐方式如下:括号跟函数名在一行 想改为括号独自占一行,如下: 配置方式如下:File->Setting-> ...
- Hadoop JobTracker和NameNode运行时参数查看
1)JobTracker运行时参数: hadoop@ubuntu:/home/zhangchao3$ ps -ef | grep job hadoop 29563 1 0 11:34 pts/12 ...
- RecyclerView 与 ItemTouchHelper 实现拖拽效果
截图 需求 App 开发新的需求,要求 RecyclerView 实现的九宫格样式可以拖拽,松手以后变更位置,类似于手机桌面拖动 app 变更位置. 分析 经过搜索,发现 support 中带有一个类 ...
- eclipse 运行 emulator时,PANIC:Could not open emulator 的解决办法
使用eclipse启动emulator的时候,出现PANIC:Could not open emulator,模拟器无法正常的运行. 经过搜索得知,因为我的SDK的环境变量出问题,需要重新配置下环境变 ...
- Java常考面试题(二)
序言 昨天刚开始的”每日5题面试“这类文章,感觉还不错,把一些平常看似懂了的东西,弄清楚了.就像什么是虚拟机?这个问题,看起来知道,但是要说出个所以然来,又懵逼了,经常回过头来看看做过的面试题,试着用 ...
- 分析 ThreadLocal 内存泄漏问题
ThreadLocal 的作用是提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量的传递的复杂度.但是如果滥用 ThreadLocal,就可能会导 ...
- [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5
.NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5 This tutorial series sh ...
- [Windows Azure]The Autoscaling Application Block
The Autoscaling Application Block 5 out of 6 rated this helpful - Rate this topic ...
- python(41):copy拷贝(深拷贝deepcopy与浅拷贝copy)
Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块. 1.copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象. 2.copy.deepco ...