#出错信息ide
##fatal error: all goroutines are asleep - deadlock! 出错信息的意思是:
在main goroutine线,指望从管道中得到一个数据,而这个数据必须是其余goroutine线放入管道的
可是其余goroutine线都已经执行完了(all goroutines are asleep),那么就永远不会有数据放入管道。
因此,main goroutine线在等一个永远不会来的数据,那整个程序就永远等下去了。
这显然是没有结果的,因此这个程序就说“算了吧,不坚持了,我本身自杀掉,报一个错给代码做者,我被deadlock了”code
例子:变量
package main func main() { c := make(chan bool) go func() { c <- true }() <-c //这里从c管道,取到一个true <-c //这行致使deadlock,由于这时的c管道,永远都取不到数据(注释掉这行就不报错) }
##no new variables on left side of := 出错信息的意思是:
:=
的意思是声明+赋值
,声明过的变量不能从新声明,因此第二行只能用赋值符号=
例子:程序
package main func main() { a := 1 a := 2 //错误,应该为a = 2 }