Skip to content
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

proposal: error handling: #68158

Closed
thorntonrose opened this issue Jun 24, 2024 · 4 comments
Closed

proposal: error handling: #68158

thorntonrose opened this issue Jun 24, 2024 · 4 comments
Labels
Milestone

Comments

@thorntonrose
Copy link

Proposal Details

Would you consider yourself a novice, intermediate, or experienced Go programmer?

Experienced.

What other languages do you have experience with?

Java, Groovy, Python, Erlang, Ruby, Delphi, FoxPro, C/C++, and others.

Would this change make Go easier or harder to learn, and why?

Neither. No language changes are included in this proposal.

Has this idea, or one like it, been proposed before?

Not that I am aware of.

What is the proposed change?

Simply say, in documentation and training materials (e.g. "Effective Go", "Go Style Best Practices"), that panic/recover is as acceptable as returning errors.

Is this change backward compatible?

Yes. No language changes are required.

Examples:

Idiomatic / good style:

func GetProfile(req Request) (Response, error) {
   user, err := db.GetUserById(req.Id)

   if err != nil {
      return Response{}, err
   }

   settings, err := db.GetSettingsById(req.Id)

   if err != nil {
      return Response{}, err
   }

   return Response{User: user, Settings: settings}, nil
}

Also idiomatic / good style:

func GetProfile(req Request) (resp Response, err error) {
   defer func() {
      if r := recover(); r != nil {
         err = fmt.Errorf("get profile: %w", r)
      }
   }()

   resp.User := db.GetUserById(req.Id)
   resp.Settings := db.GetSettingsById(req.Id)

   return resp, err
}
package db

...

func GetUserById(id string) User {
   user := ...

   if user == nil {
      panic(fmt.Errorf("user not found: %s", id))
   }

What is the cost of this proposal?

Minimal. Only documentation and training materials would need to be updated.

Can you describe a possible implementation?

N/A

How would the language spec change?

N/A

Orthogonality: how does this change interact or overlap with existing features?

N/A

Is the goal of this change a performance improvement?

No.

Does this affect error handling?

In the language, no. In solutions, perhaps.

*Is this about generics?

No.

Conclusion:

Panic and recover are features of the language. There is no reason that using them should be any less valid than using any other language feature. Making panic/recover as acceptable as returning errors would make error handling a less contentious subject and greatly reduce the desire of many programmers to "fix" it (or to just stop using Go).

@gopherbot gopherbot added this to the Proposal milestone Jun 24, 2024
@seankhliao
Copy link
Member

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2024
@thorntonrose
Copy link
Author

See https://go.dev/blog/errors-are-values

I have seen that, many times. Dogma is alive and well here.

@ianlancetaylor
Copy link
Contributor

Go is an opinionated language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants