Go-内置函数之append、recover用法
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
总结:输出实时异常结果,不利于排查详细问题。应打出详细信息输出到日志里。