Last active
October 6, 2019 20:51
-
-
Save yaaase/c3201392d00156cf5a2fd75a1f2b1162 to your computer and use it in GitHub Desktop.
baderrors.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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