Raise exception

I would like to throw an exception that was made in Python or Java - to end the program with an error message -.

The error message may be returned to the parent function:

func readFile(filename string) (content string, err os.Error) {
    content, err := ioutil.ReadFile(filename)
    if err != nil {
        return "", os.ErrorString("read " + filename + ": " + err)
    }
    return string(content), nil
}

but I want it to be able to complete when an error is found. Will the following be correct?

func readFile(filename string) (content string) {
    content, err := ioutil.ReadFile(filename)

    defer func() {
        if err != nil {
            panic(err)
        }
    }()

    return string(content)
}
+3
source share
1 answer

, Go . panic recover, , . , . - , nil .

+8

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