Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustPanic ¶
func MustPanic(testFunc func())
MustPanic 有錯誤才是正常,沒有錯誤會引發panic("should panic") If you know that you will get a panic, then you can call this function that will do recover.
Example ¶
If you know that you will get a panic, then you can call this function that will do recover.
MustPanic(func() {
panic("raise a panic")
})
fmt.Println("I can run in here")
Output: I can run in here
func TestPanic ¶
TestPanic 在測試的時候,我們會預期有panic,但如果想要確定panic的原因是否真的如同我們猜測,就可以使用這個函數幫忙
Example ¶
// example1
reason, isPanic := TestPanic(func() {
panic("invalid memory address")
})
fmt.Printf("%v | %v\n", reason, isPanic)
// example2
reason, isPanic = TestPanic(func() {
panic(errors.New("reason type: error"))
})
fmt.Printf("%v | %v\n", reason.(error).Error(), isPanic)
// example3
reason, isPanic = TestPanic(func() {
_ = fmt.Sprintln("hello world")
})
fmt.Printf("%v | %v\n", reason, isPanic)
Output: invalid memory address | true reason type: error | true <nil> | false
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.