-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add an error example #62
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution @rexagod
// a float64 and error value based on the input | ||
func (N number) sqrt() (val float64, err error) { | ||
if N.n < 0 { | ||
err = errors.New("Negative value!") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err = errors.New("Negative value!") | |
err = errors.New("negative value") |
Error strings should not be capitalized or end with punctuation. See https://github.com/golang/go/wiki/CodeReviewComments#error-strings
|
||
// define a sqrt function on "number" type that returns | ||
// a float64 and error value based on the input | ||
func (N number) sqrt() (val float64, err error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please merge this example with the one above by replacing the doStuff
function.
Also, this struct is not really for this example. Let's define it only as a single function.
Thanks
func main() { | ||
N := number{-1} | ||
_, err := N.sqrt() | ||
fmt.Print(err == nil) // false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on top of what @a8m suggested, it'd also be better to demo err.Error()
as well.
fmt.Print(err == nil) // false | |
fmt.Print(err == nil) // false | |
fmt.Print(err.Error()) // negative value |
Merged examples and removed struct. Continuation of a8m#62 since I deleted that branch earlier.
Merged examples and removed struct. Continuation of a8m#62 since I deleted that branch earlier.
Merged examples and removed struct. Continuation of a8m#62 since I deleted that branch earlier.
Merged examples and removed struct. Continuation of a8m#62 since I deleted that branch earlier.
Merged examples and removed struct. Continuation of #62 since I deleted that branch earlier.
I didn't find the current "error" section to be convincing in it's expression and semantics, hence I added this so anyone can quickly get a clear idea about it.
PS. Thank you for this cheat sheet!