What is the difference between panic ("error_msg") and panic (error.New ("error_msg")?

Given that I am using the source package "errors" go.

And the difference between panic (11) and panic ("11")?

+4
source share
2 answers

panicdefines how func panic(v interface{}), when called panic(anything), the string representation will be printed anything, and then the calling function stouttrace.

The only difference is that if you use recover, you can access everything that you panicked, for example :

func main() {
    defer func() {
        if err := recover(); err != nil {
            if n, ok := err.(int); ok && n == 11 {
                fmt.Println("got 11!")
            }
        }
    }()
    panic(11)
}
+5
source

panic("error_msg")and panic("11")panic a string, and panic(error.New("error_msg")fend off an error and panic(11)panic an integer.

recover defer, , , "error_msg" "11".

+3

Source: https://habr.com/ru/post/1546855/


All Articles