1、

fatal error: all goroutines are asleep - deadlock!

所有的协程都休眠了 - 死锁!

package main
import("fmt")
func f1(in chan int){
fmt.Println(<-in)
}
func main(){
out :=make(chan int)
out<-2
go f1(out)
}

package main
import("fmt")
func f1(in chan int){
fmt.Println(<-in)
}
func main(){
out :=make(chan int)
// out<-2
go f1(out)
out<-2
}

Understanding the context package in golang - Parikshit Agnihotry http://p.agnihotry.com/post/understanding_the_context_package_in_golang/

  package main

  import "fmt"

  //prints to stdout and puts an int on channel
func printHello(ch chan int) {
fmt.Println("Hello from printHello")
//send a value on channel
ch <- 2
} func main() {
//make a channel. You need to use the make function to create channels.
//channels can also be buffered where you can specify size. eg: ch := make(chan int, 2)
//that is out of the scope of this post.
ch := make(chan int)
//inline goroutine. Define a function and then call it.
//write on a channel when done
go func(){
fmt.Println("Hello inline")
//send a value on channel
ch <- 1
}()
//call a function as goroutine
go printHello(ch)
fmt.Println("Hello from main") //get first value from channel.
//and assign to a variable to use this value later
//here that is to print it
i := <- ch
fmt.Println("Recieved ",i)
//get the second value from channel
//do not assign it to a variable because we dont want to use that
<- ch
}

  

Hello from main
Hello from printHello
Recieved 2
Hello inline

golang channel 使用总结 - litang.me http://litang.me/post/golang-channel/

package main

import (
"fmt"
"time"
)

func fA(ch chan bool) {
defer func() {
fmt.Println("fA-defer")
}()
fmt.Println("fA-doing")
ch <- true
time.Sleep(time.Duration(123) * time.Millisecond)

}

func fB(ch chan bool) {
defer func() {
fmt.Println("fB-defer")
}()
fmt.Println("fB-doing")
ch <- true

}

func main() {
defer func() {
fmt.Println("main-defer")
}()
ch := make(chan bool)
go fA(ch)
go fB(ch)
<-ch
<-ch
}

目前打印结果为:
fB-doing
fB-defer
fA-doing
main-defer
修改代码打印结果为:
fB-doing
fB-defer
fA-doing
fA-defer
main-defer

交换

ch <- true
time.Sleep(time.Duration(123) * time.Millisecond)

位置

最新文章

  1. [POJ3111]K Best(分数规划, 二分)
  2. go语言常用函数:copy
  3. [转]REST简介
  4. linux查看rpm包创建的所有目录和文件
  5. 从Java的角度理解前端框架,nodejs,reactjs,angularjs,requirejs,seajs
  6. C#基础练习(使用委托窗体传值)
  7. 微软数学库XNAMATH(DirectXMath)
  8. 未来 USB Type-C 将可靠软体判断线材是否符合规定
  9. 【转】【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法
  10. windows服务-log4net的使用
  11. ssl证书验证
  12. SLAM+语音机器人DIY系列:(二)ROS入门——3.在ubuntu16.04中安装ROS kinetic
  13. 【RL-TCPnet网络教程】第20章 RL-TCPnet之BSD Socket客户端
  14. NPOI 的使用姿势
  15. 用Python自动发送邮件
  16. python模块之HTMLParser抓页面上的所有URL链接
  17. ffmpeg 播放音频
  18. 使用手机预览移动端项目(Vue)
  19. codevs 1214 线段覆盖
  20. 记录memcache分布式策略及算法

热门文章

  1. 查看权限详情 将部门大类单据整合,将子类单据id去重合并
  2. [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了
  3. JavaDailyReports10_05
  4. Nginx压力测试问题
  5. 【linux】系统编程-5-线程
  6. MySQL 中的WAL机制
  7. idea配置scala编写spark wordcount程序
  8. github下载大文件太慢/失败
  9. 如何快速搭建hadoop集群
  10. Nginx安装,开箱即用?