我的Go函数应该返回一个值,但在调用库函数时可能会出现混乱.我可以用来recover()在延期调用中捕获它,但是在这种情况下如何返回值?
func MyFunc() string{
defer func() {
if err := recover(); err != nil {
// What do I do to make MyFunc() return a value in case of panic?
}
}()
SomeFuncThatMayPanic()
return "Normal Return Value"
// How can I return "ERROR" in case of panic?
}
Run Code Online (Sandbox Code Playgroud)