go語言報錯信息

#出錯信息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
}
相關文章
相關標籤/搜索