github.com/nibnait/go-learn@v0.0.0-20220227013611-dfa47ea6d2da/src/test/chapter/ch3/21_panic_recover_test.go (about) 1 package ch3 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "testing" 8 ) 9 10 func TestPanicVxExit1(t *testing.T) { 11 defer func() { 12 fmt.Println("Finally!") 13 }() 14 fmt.Println("Start") 15 //panic(errors.New("Something wrong!")) 16 os.Exit(-1) 17 fmt.Println("End") 18 } 19 20 func TestPanicVxExit2(t *testing.T) { 21 defer func() { 22 if err := recover(); err != nil { 23 // 相当于 catch 里面,进行回滚操作 24 // ⚠️ 危险!!当⼼ recover 成为恶魔,形成僵⼫服务进程,导致 health check 失效。 25 fmt.Println("回滚 ", err) 26 } 27 }() 28 fmt.Println("Start") 29 panic(errors.New("Something wrong!")) 30 //os.Exit(-1) 31 fmt.Println("End") 32 }