Go-內置函數之append、recover用法
阿新 • • 發佈:2018-11-13
pri sleep ack 輸出結果 print return err 用法 erro
package main import "fmt" import "time" func test() { defer func() { if err := recover(); err != nil { fmt.Println(err) } }() b := 0 a := 100 / b fmt.Println(a) return } func main() { for { test() time.Sleep(time.Second) } var a []int a = append(a,10,20,333) a = append(a,a...) fmt.Println(a) } ---輸出結果---
E:\Go\project>haha1.exe
runtime error: integer divide by zero
runtime error: integer divide by zero
runtime error: integer divide by zero
runtime error: integer divide by zero
總結:輸出實時異常結果,不利於排查詳細問題。應打出詳細信息輸出到日誌裏。
Go-內置函數之append、recover用法