我創造,我運行功能同時裏面,我恐慌,恢復的例子:通恢復到上夠程golang
package main
import "fmt"
func main() {
// "main" recovery
defer func() {
if r := recover(); r != nil {
fmt.Println("main goroutine paniced:", r)
}
}()
// running function concurrently inside which I panic
chanStr := make(chan string)
go func() {
// this "internal" goroutin recovery
defer func() {
if r := recover(); r != nil {
fmt.Println("internal goroutine paniced:", r)
}
chanStr <- "hello world"
}()
// panicking and wanting recovery not only in "internal" recovery but in "main" recovery as well
panic("NOT main goroutine")
}()
// waiting for chan with "internal" goroutine panicking and recovery
str := <-chanStr
fmt.Println(str)
// panic("main")
}
它給輸出:
internal goroutine panicked: NOT main goroutine
hello world
是否可以更改我的代碼從「內部」恢復到「主要」?換句話說,我希望它寫下來到控制檯:
internal goroutine paniced: NOT main goroutine
main goroutine paniced: main
hello world
我試圖在所有消除「內部」復甦FUNC實現這一點,但「主」恢復不恢復裏面的「內部」夠程恐慌在這案件。
更新
我試圖按照@ Momer的建議,並通過通道發送一個錯誤,並在主要的goroutine處理它,而不是試圖泡沫恐慌起來:
package main
import (
"errors"
"fmt"
)
func main() {
// "main" recovery
defer func() {
if r := recover(); r != nil {
fmt.Println("main goroutine paniced:", r)
}
}()
// running func concarantly inside which I panic
chanStr := make(chan string)
chanErr := make(chan error)
var err error
go func() {
// this "internal" goroutin recovery
defer func() {
if r := recover(); r != nil {
fmt.Println("internal goroutine paniced:", r)
switch t := r.(type) {
case string:
fmt.Println("err is string")
err = errors.New(t)
case error:
fmt.Println("err is error")
err = t
default:
fmt.Println("err is unknown")
err = errors.New("Unknown error")
}
chanErr <- err
chanStr <- ""
}
}()
// panicing and wanting recovery not only in "internal" recovery but in "main" recovery as well
panic("NOT main goroutine")
chanStr <- "hello world"
chanErr <- nil
}()
// waiting for chan with "internal" goroutin panicing and recovery
str := <-chanStr
err = <-chanErr
fmt.Println(str)
fmt.Println(err)
// panic("main")
}
它給錯誤
all goroutines are asleep - deadlock
FUL l輸出:
go run /goPath/parentRecoverty2.go
internal goroutine paniced: NOT main goroutine
err is string
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
/goPath/parentRecoverty2.go:48 +0x1d4
goroutine 5 [chan send]:
main.func·002()
/goPath/parentRecoverty2.go:37 +0x407
main.func·003()
/goPath/parentRecoverty2.go:42 +0x130
created by main.main
/goPath/parentRecoverty2.go:46 +0x190
exit status 2
而不是試圖讓恐慌起來,爲什麼不通過通道發送錯誤,並在主要goroutine中處理它? – Momer 2015-04-05 13:52:26
@Momer我試過了,但有錯誤'所有goroutines都睡着了 - 死鎖',請看我上面的更新。 – 2015-04-05 15:20:54
第二次寫入(在恢復中)塊,因爲沒有人可以從通道讀取。你將要多次寫入它們,但只能讀一次。 – flx 2015-04-05 16:27:29