在Go語言,error是一個接口類型。error接口類型是作為錯誤處理的標準模式,如果函數要返回錯誤,則返回值類型列表中肯定包含error;error處理過程類似于C語言中的錯誤碼,可逐層返回,直到被處理。error接口類型定義為僅包含一個方法的 Error() string;所有實現該接口的類型都可以當作一個錯誤類型。
本教程操作環境:windows7系統、GO 1.18版本、Dell G3電腦。
Go 錯誤是指程序執行過程中遇到與設計流程不相符的情況,引發的人為的或自動的反饋機制。有些錯誤有有意設計的,并增加了錯誤處理,或者反饋給用戶等待處理,例如檢查到除數為 0,會報錯誤,使得用戶可以認識到自己的輸入的問題,再如爬取指定頁面信息的代碼遇到了網絡斷開的情況;而另外錯誤則是程序設計考慮不周導致的BUG,比如數組訪問下標越界,空指針操作導致崩潰等等。針對各種情況設計良好的錯誤處理是代碼成熟的標志之一,也是必須積累的經驗或者說需要周密的設計。
Go語言的error類型
Go 錯誤使用 error 表示,是一個接口類型,通常都是跟返回值一起聲明的。
錯誤處理在每個編程語言中都是一項重要內容,通常開發中遇到的分為異常與錯誤兩種,Go語言中也不例外。
在C語言中通過返回 -1 或者 NULL 之類的信息來表示錯誤,但是對于使用者來說,如果不查看相應的 API 說明文檔,根本搞不清楚這個返回值究竟代表什么意思,比如返回 0 是成功還是失敗?
針對這樣的情況,Go語言中引入 error 接口類型作為錯誤處理的標準模式,如果函數要返回錯誤,則返回值類型列表中肯定包含 error。error 處理過程類似于C語言中的錯誤碼,可逐層返回,直到被處理。
error 接口類型定義為僅包含一個方法的 Error() string。所有實現該接口的類型都可以當作一個錯誤類型。Error() 方法給出了錯誤的描述。這意味著可以給所有數據類型都配備錯誤類型。
//The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error. type error interface { Error() string }
//DNSError represents a DNS lookup error. type DNSError struct { Err string // description of the error Name string // name looked for Server string // server used IsTimeout bool // if true, timed out; not all timeouts set this IsTemporary bool // if true, error is temporary; not all errors set this; added in Go 1.6 } func (e *DNSError) Error() string func (e *DNSError) Temporary() bool //Temporary reports whether the DNS error is known to be temporary. This is not always known; a DNS lookup may fail due to a temporary error and return a DNSError for which Temporary returns false. func (e *DNSError) Timeout() bool //Timeout reports whether the DNS lookup is known to have timed out. This is not always known; a DNS lookup may fail due to a timeout and return a DNSError for which Timeout returns false.
具體看 *DNSError 就可以體會一個錯誤類型的定義。 *DNSError 包含5個字段結構體。Err 描述錯誤文字,Name 為查詢的域名,Server 服務器用,IsTimeout 和 IsTemporary 為指示錯誤原因的兩個布爾量。用下面的例子具體體會。
func main() { name := "www.ilydsssss.com" addr, err := net.LookupHost(name) if errS, ok := err.(*net.DNSError); ok { fmt.Printf("%+vn", *errS) fmt.Println(err) } else { fmt.Println(name, addr) } } /* result for ------www.ilydsssss.com------------ {Err:no such host Name:www.ilydsssss.com Server: IsTimeout:false IsTemporary:false} lookup www.ilydsssss.com: no such host ------------ www.iloveyou.com------------ {Err:getaddrinfow: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. Name:www.iloveyou.com Server: IsTimeout:false IsTemporary:false} lookup www.iloveyou.com: getaddrinfow: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. 傳說中的發送DNS,沒有返回的結果,原因你懂的, 這個是什么站點,noidea ----------- www.baidu.com ------------ www.baidu.com [180.97.33.108 180.97.33.107]
上述用例中,如果查詢失敗(就是指針不為nil),則進行了類型斷言,如果是 *net.DNSError 指針,則打印結構體字段,并輸出錯誤;否則打印域名和地址。可以看到定義兩類錯誤中,上述查詢并沒有返回任何一種,但確實錯誤了。
同時,也可猜測,func (e *DNSError) Error() string 的定義就是 return "look " + e.Name + e.Err。
error 的創建
Go 內部的錯誤反饋就是這樣定義,如何定義新的錯誤類型呢。
-
定義結構體,實現 error 接口
新建一個結構體,仿照上述 DNSError 建立一個需要保存錯誤的結構,同時實現 error 接口,就可以實現。
-
error.New()函數
package errors // New returns an error that formats as the given text. func New(text string) error { return &errorString{text} } // errorString is a trivial implementation of error. type errorString struct { s string } func (e *errorString) Error() string { return e.s }
errorString 是一個僅包含了一個字符串的結構體類型,同時實現了error接口,New() 函數只是利用一個錯誤描述的字符串初始化errorString并返回該結構體地址,這就使得一個簡單的錯誤類型,可以隨時被直接調用,而不用創建一個結構體并實現接口,如果需要,那就用方法1。
-
利用 fmt.Errorf() 返回 error 接口
fmt.Errorf() 函數簽名:func Errorf(format string, a …interface{}) error,它利用一個格式化的字符串,利用上述方法,返回一個簽名。是否還記得 func Sprintf(format string, a …interface{}) string, fmt.Errorf()實現也僅僅 return error.New(fmt.Sprintf(format string, a …interface{}))
錯誤處理
當寫一個庫時,如果發生一個錯誤,一種方式就是按照上述所說,拋出一個錯誤,由上層或用戶去決斷如何處理,是退出還是提示修改;另一種方式就是拋出 panic 來終止程序,除非遇到特別嚴重的錯誤,什么叫嚴重呢?就是程序已經沒有執行的必要了,莫不如拋出錯誤,直接退出。有兩種情況可以考慮使用 panic: 1. 發生了一個不能恢復的錯誤,此時程序不能繼續運行。2. 存在一個編程上的錯誤。
當程序由 panic 引發終止時,可以使用 recover 重新獲取該程序控制權。panic 和 recover 與其他語言中的 try-catch-finally 語句類似,只不過一般我們很少使用 panic 和 recover。
內建函數 panic 的簽名為:func panic(interface{}),此處接口為空接口,也可以理解為任意數據類型都可以輸入,輸入什么,則提示什么。
func div(x, y int) float64 { defer fmt.Println("DIV ......") if y == 0 { panic(fmt.Sprintf("%d / %d, 除數為零, 無法計算", x, y)) } return float64(x) / float64(y) } fmt.Println(div(3, 0)) /* result panic: 3 / 0, 除數為零 goroutine 1 [running]: main.div(0x3, 0x0, 0x2) error.go:10 +0x148 main.main() error.go:25 +0x15a exit status 2 */
從上述例子可以看到,當函數發生 panic 時,它會終止運行,在執行完所有的延遲函數后,程序控制返回到該函數的調用方。這樣的過程會一直持續下去,直到當前協程的所有函數都返回退出,然后程序會打印出 panic 信息,接著打印出堆棧跟蹤,最后程序終止。
recover 是一個內建函數,用于重新獲得 panic 協程的控制。recover 函數的標簽如下所示:func recover() interface{}。需要注意的是:只有在延遲函數的內部,調用 recover 才有用。在延遲函數內調用 recover,可以取到 panic 的錯誤信息,并且停止 panic 續發事件,程序運行恢復正常。如果在延遲函數的外部調用 recover,就不能停止 panic 續發事件。
例如:
import ( "runtime/debug" ) func recoverFdiv() { if r := recover(); r != nil { fmt.Println("來自 DIV 的恢復, 除數為零,下面是出錯log記錄") debug.PrintStack() } } func div(x, y int) float64 { defer recoverFdiv() if y == 0 { panic(fmt.Sprintf("%d / %d, 除數為零, 無法計算", x, y)) } return float64(x) / float64(y) } fmt.Println(div(3, 0)) /* result 來自 DIV 的恢復, 除數為零,下面是出錯log記錄 goroutine 1 [running]: runtime/debug.Stack(0xc000072008, 0xc00006fd68, 0x1) runtime/debug/stack.go:24 +0xae runtime/debug.PrintStack() runtime/debug/stack.go:16 +0x29 main.recoverFdiv() D:/ZHY-L/OneDrive/文檔/開發/goblog/myerror.go:12 +0x89 panic(0x4b9620, 0xc000030040) runtime/panic.go:513 +0x1c7 main.div(0x3, 0x0, 0x0) error.go:19 +0x186 main.main() error.go:34 +0x15a 0 */
如上所示,調用延遲函數 recoverFdiv(),它使用了 recover() 來停止 panic 續發事件,主函數還是繼續執行了。同時,利用debug.PrintStack() 打印了 panic 記錄,這樣在保證程序繼續執行的同時,也留下了調試寶貴的記錄。
同理,Go 內置的運行時錯誤(如數組越界)也會導致 panic。這等價于調用了內置函數 panic,其參數由接口類型 runtime.Error 給出。runtime.Error 接口的定義如下:
type Error interface { error // RuntimeError is a no-op function but // serves to distinguish types that are run time // errors from ordinary errors: a type is a // run time error if it has a RuntimeError method. RuntimeError() }
【