Skip to content

error: it's really easy make mistake that return custom error assign to defined interface error #20389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tnclong opened this issue May 17, 2017 · 2 comments

Comments

@tnclong
Copy link
Contributor

tnclong commented May 17, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.8 darwin/amd64

package main

import (
	"fmt"
)

type ValidationError struct {
	Code string
	Msg  string
}

func (e ValidationError) Error() string {
	return e.Code + e.Msg
}

func ReturnErr() *ValidationError {
	return nil
}

func ReturnInterfaceErr() error {
	var err *ValidationError
	return err
}

func main() {
	var err1 error
	err1 = ReturnErr()

	var err2 error = ReturnErr()

	var err3 = ReturnErr()

	// err1 false
	fmt.Println("err1", err1 == nil)
	// err2 false
	fmt.Println("err2", err2 == nil)
	// err3 true
	fmt.Println("err3", err3 == nil)

	var err4 error
	err4 = ReturnInterfaceErr()

	// err4 false
	fmt.Println("err4", err4 == nil)

}
@ghost
Copy link

ghost commented May 17, 2017

Is there a proposal here? This scenario is covered in the FAQ: https://golang.org/doc/faq#nil_error

@ianlancetaylor
Copy link
Contributor

Closing because there is no suggested action.

@golang golang locked and limited conversation to collaborators May 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants