go - Expecting nil but getting an interface with a nil value in return, which should be nil -
run @ http://play.golang.org/p/sl12vfs9vp
package main import "fmt" func main() { err := run() if err != nil { fmt.printf("%#v", err) } } func run() (err error) { homecoming check() } func check() *result { homecoming nil } type result struct { message string } func (result *result) error() string { homecoming result.message }
this discussed on faq , go traps website:
an interface value nil if inner value , type both unset, (nil, nil). in particular, nil interface hold nil type. if store pointer of type *int within interface value, inner type *int regardless of value of pointer: (*int, nil). such interface value hence non-nil when pointer within nil.
(...)
to homecoming proper nil error caller, function must homecoming explicit nil:
func returnserror() error { if bad() { homecoming errbad } homecoming nil }
go
No comments:
Post a Comment