Assuming we have this test:
func TestRecover(t *testing.T) {
f := func() (interface{}, error) {
panic(`TEST`)
}
r, e := Recover(f)
t.Log(r, e)
}
And two versions of *Recover* function.
Version 1:
func Recover(f func() (interface{}, error)) (interface{}, error) {
var result interface{}
var err error
defer func() {
if e := recover(); e != nil {
err = errors.Error(fmt.Sprintf("%v", e))
}
}()
result, err = f()
return result, err
}
Version 2:
func Recover(f func() (interface{}, error)) (res interface{}, err error) {
defer func() {
if e := recover(); e != nil {
err = errors.Error(fmt.Sprintf("%v", e))
}
}()
res, err = f()
return res, err
}
*Question:* Why the output of test for Version 1 is *<nil> <nil>* (*not
expected/wrong*) but for Version 2 is *<nil> TEST* (*as expected/correct*)?
- Go 1.7, Ubuntu 14.04 x64
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.