#出错信息
##fatal error: all goroutines are asleep - deadlock! 出错信息的意思是:
在main goroutine线,期望从管道中获得一个数据,而这个数据必须是其他goroutine线放入管道的
但是其他goroutine线都已经执行完了(all goroutines are asleep),那么就永远不会有数据放入管道。
所以,main goroutine线在等一个永远不会来的数据,那整个程序就永远等下去了。
这显然是没有结果的,所以这个程序就说“算了吧,不坚持了,我自己自杀掉,报一个错给代码作者,我被deadlock了”
例子:
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
}