Skip to content

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

Closed
wants to merge 1 commit into from
Closed

Add an error example #62

wants to merge 1 commit into from

Conversation

rexagod
Copy link
Contributor

@rexagod rexagod commented Apr 3, 2021

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!

Copy link
Owner

@a8m a8m left a 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!")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Owner

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
Copy link
Collaborator

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.

Suggested change
fmt.Print(err == nil) // false
fmt.Print(err == nil) // false
fmt.Print(err.Error()) // negative value

rexagod added a commit to rexagod/golang-cheat-sheet that referenced this pull request Apr 3, 2021
Merged examples and removed struct.
Continuation of a8m#62 since I deleted that branch earlier.
rexagod added a commit to rexagod/golang-cheat-sheet that referenced this pull request Apr 3, 2021
Merged examples and removed struct.
Continuation of a8m#62 since I deleted that branch earlier.
@rexagod rexagod mentioned this pull request Apr 3, 2021
@rexagod rexagod closed this Apr 3, 2021
rexagod added a commit to rexagod/golang-cheat-sheet that referenced this pull request Apr 4, 2021
Merged examples and removed struct.
Continuation of a8m#62 since I deleted that branch earlier.
rexagod added a commit to rexagod/golang-cheat-sheet that referenced this pull request Apr 4, 2021
Merged examples and removed struct.
Continuation of a8m#62 since I deleted that branch earlier.
a8m pushed a commit that referenced this pull request Apr 4, 2021
Merged examples and removed struct.
Continuation of #62 since I deleted that branch earlier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants