4 ms·
> False is not a valid value for an error... Welcome to static typing ;). I'm confused. Previously you gave this example: a, ok := m[1] // returns 0, fals
by Dewie 12y ago
> False is not a valid value for an error... Welcome to static typing ;).
I'm confused. Previously you gave this example:
a, ok := m[1] // returns 0, false
Which means that `ok == false`. But how can it be false if that is not a valid value for an error?
- NateDad 12y agoSorry to be confusing and/or confused. Map access in Go returns value, boolean where the boolean indicates if the key existed in the map. This is in contrast to most functions which can fail with a generic "error" value. So, for errors, nil means no errors. For map access, false means it didn't exist in the map. About your original point about someone returning "the wrong thing" in an error... there's really no misunderstanding possible. Because Go is statically typed, the ONLY things you can return for an error are nil (no error), or something that fulfills the error interface (a type with a method Error() that returns a string). You can't return false, or 0, or "" when the code expects you to return an error... the compiler won't let you. This is how all error handling works in Go. From the most basic program you write, you learn that err == nil means success, anything else means failure. Does that help any?