You could try it this way if you really need a separate function. https://play.golang.org/p/V-ysjWbZ2X5
On Thu, 2019-07-18 at 12:51 +0800, ZP L wrote: > Sorry for the bad formatting. > > > recover must be called directly by a deferred function > > func logPanic() { > defer func() { > if err := recover(); err != nil { > fmt.Println("got panic") > return > } > }() > } > This still not working. > > > > From https://blog.golang.org/defer-panic-and-recover, > > *Recover* is a built-in function that regains control of a panicking > goroutine. Recover is only useful inside deferred functions. During > normal > execution, a call to recover will return nil and have no other > effect. *If > the current goroutine is panicking, a call to recover will capture > the > value given to panic and resume normal execution.* > I think I didn't break any rules. > > > Ian Lance Taylor <i...@golang.org> 于2019年7月17日周三 下午9:59写道: > > > On Wed, Jul 17, 2019 at 5:11 AM Tamás Gulácsi <tgulacs...@gmail.com > > > > > wrote: > > > > > > The "recover()" call must be in the deferred part. > > > > Yes, as the spec says, recover must be called directly by a > > deferred > > function. > > > > When sending code to this list, please use a link to the Go > > playground > > or use plain text. The highlighted text with a black background is > > unreadable. Thanks. > > > > Ian > > > > -- > > You received this message because you are subscribed to a topic in > > the > > Google Groups "golang-nuts" group. > > To unsubscribe from this topic, visit > > https://groups.google.com/d/topic/golang-nuts/Ok40EBXxQ2Q/unsubscribe > > . > > To unsubscribe from this group and all its topics, send an email to > > golang-nuts+unsubscr...@googlegroups.com. > > To view this discussion on the web visit > > https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXUWYdq%3DzSSw4V_HvNzrNpnrt1awEZbRFca0f6qHoBdsQ%40mail.gmail.com > > . > > For more options, visit https://groups.google.com/d/optout. > > > > > -- > --刘志平 > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c93def05cdb72cb9351eecd39087969dc9429f97.camel%40kortschak.io. For more options, visit https://groups.google.com/d/optout.