panic 抛出异常 通过recover捕获
类似 php python等语言的try catch
package main
import ( "fmt" "errors" )
func main() {
testPanic()
}
func testPanic() { defer coverPanic()
//panic("i am panic") panic(errors.New(" i am a error")) }
func coverPanic() { message := recover() switch message.(type) { case string: fmt.Println("string message",message)
case error: fmt.Println("error message",message) default: fmt.Println("unknown message",message) } }
|
请发表评论