Skip to content

Instantly share code, notes, and snippets.

@yaaase
Last active October 6, 2019 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaaase/c3201392d00156cf5a2fd75a1f2b1162 to your computer and use it in GitHub Desktop.
Save yaaase/c3201392d00156cf5a2fd75a1f2b1162 to your computer and use it in GitHub Desktop.
baderrors.go
package main
import "fmt"
// make a custom error type
type MyError struct{}
// satisfy the Error interface
func (e *MyError) Error() string {
return ""
}
func customErrorReturnWithType() *MyError {
var err *MyError // if you are unfamiliar with go, declaring but not intializing a variable like this
return err // initializes to what is called the "zero value", which is nil for pointer types
}
func customErrorReturn() error {
var err *MyError
return err
}
func main() {
sane := customErrorReturnWithType() // return type is '*MyError'
fmt.Printf("%v\n", sane == nil) // it's nil
insane := customErrorReturn() // return type is just 'error'
fmt.Printf("%v\n", insane == nil) // wat
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment