That's kind of expected behavior , "*1. A deferred function's arguments are evaluated when the defer statement is evaluated." * refer to https://blog.golang.org/defer-panic-and-recover
On Wednesday, March 29, 2017 at 9:48:52 AM UTC+11, Stefan Engstrom wrote: > > Playing with recovery from a panic - this code does what i expect: ( > https://play.golang.org/p/p5KvOYc1sx) > > package main > > import "fmt" > > func main() { > defer func() { > fmt.Println("main:",recover()) > }() > a() > } > > func a() { > panic("yikes") > } > > Results in: > > main: yikes > > > > > A simpler version without the closure doesn't catch the panic: ( > https://play.golang.org/p/0ICqqdi2kL) > > package main > > import "fmt" > > func main() { > defer fmt.Println("main:",recover()) > a() > } > > func a() { > panic("yikes") > } > > Produces: > > main: <nil> > panic: yikes > > goroutine 1 [running]: > main.a() > /tmp/sandbox638763687/main.go:11 +0x60 > main.main() > /tmp/sandbox638763687/main.go:7 +0x100 > > > I see the same behavior on linux/amd64 go1.8 > > Any idea why the second version fails to catch that panic? > > Thanks, > > Stefan > > -- 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. For more options, visit https://groups.google.com/d/optout.