-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
Nil is not nill in some cases #70364
Comments
Sorry looks like it is "normal" behaviour 🤦 |
After learning FAQ I found more general issue. This behaviour makes nill unprocessable in some normal way package main
import (
"fmt"
)
type Unwrapable interface {
Unwrap() error
}
type TestStruct struct {
err error
}
func (err *TestStruct) Unwrap() error {
return err.err
}
func doSomethingElse() *TestStruct {
return nil // nil is normal value for pointer
}
func HandleUnwrapable(err Unwrapable) {
// I cannot handle nil here ever
if err != nil {
fmt.Printf("err: Not nil, type: %T, address: %p\n", err, err)
} else {
fmt.Printf("err: Nil, type: %T, address: %p\n", err, err)
}
}
func main() {
test := doSomethingElse()
if test != nil {
fmt.Printf("err: Not nil, type: %T, address: %p\n", test, test)
} else {
fmt.Printf("err: Nil, type: %T, address: %p\n", test, test)
}
HandleUnwrapable(test)
// Output
// err: Nil, type: *main.TestStruct, address: 0x0
// err: Not nil, type: *main.TestStruct, address: 0x0
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Go version
go version go1.23.1 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
I wrote custom error
What did you see happen?
When a fuction return nil typed as my custom error, but stored as error{} it is not equal to nill
What did you expect to see?
I expect nil to be equal to nil.
The text was updated successfully, but these errors were encountered: