The defer is not being run directly as a result of the panic. >From the spec:
> The recover function allows a program to manage behavior of a > panicking goroutine. Suppose a function G defers a function D that > calls recover and a panic occurs in a function on the same goroutine > in which G is executing. When the running of deferred functions > reaches D, the return value of D's call to recover will be the value > passed to the call of panic. If D returns normally, without starting > a new panic, the panicking sequence stops. 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/5f7558fc749fc840a273bc34ecd5a3ece808eeb3.camel%40kortschak.io. For more options, visit https://groups.google.com/d/optout.