mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-12-25 06:18:56 +00:00
ch5-10 fmt code
This commit is contained in:
parent
5c288d0893
commit
45b04fc567
@ -35,15 +35,15 @@ deferred函數幫助Parse從panic中恢複。在deferred函數內部,panic val
|
||||
func soleTitle(doc *html.Node) (title string, err error) {
|
||||
type bailout struct{}
|
||||
defer func() {
|
||||
switch p := recover(); p {
|
||||
case nil:
|
||||
// no panic
|
||||
case bailout{}:
|
||||
// "expected" panic
|
||||
err = fmt.Errorf("multiple title elements")
|
||||
default:
|
||||
panic(p) // unexpected panic; carry on panicking
|
||||
}
|
||||
switch p := recover(); p {
|
||||
case nil:
|
||||
// no panic
|
||||
case bailout{}:
|
||||
// "expected" panic
|
||||
err = fmt.Errorf("multiple title elements")
|
||||
default:
|
||||
panic(p) // unexpected panic; carry on panicking
|
||||
}
|
||||
}()
|
||||
// Bail out of recursion if we find more than one nonempty title.
|
||||
forEachNode(doc, func(n *html.Node) {
|
||||
@ -65,4 +65,4 @@ func soleTitle(doc *html.Node) (title string, err error) {
|
||||
|
||||
有些情況下,我們無法恢複。某些致命錯誤會導致Go在運行時終止程序,如內存不足。
|
||||
|
||||
**練習5.19:** 使用panic和recover編寫一個不包含return語句但能返迴一個非零值的函數。
|
||||
**練習5.19:** 使用panic和recover編寫一個不包含return語句但能返迴一個非零值的函數。
|
Loading…
Reference in New Issue
Block a user