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: Go 2: Better way for error handling #64674

Closed
5 of 11 tasks
Kotov584 opened this issue Dec 12, 2023 · 1 comment
Closed
5 of 11 tasks

proposal: Go 2: Better way for error handling #64674

Kotov584 opened this issue Dec 12, 2023 · 1 comment
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@Kotov584
Copy link

Kotov584 commented Dec 12, 2023

Proposal Details

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

  • Novice
  • Intermediate
  • Experienced

What other languages do you have experience with?

  • Python
  • C#

Would this change make Go easier or harder to learn, and why?
This change is intended to make Go easier to use, especially for those who are familiar with languages that provide more advanced error handling syntax. It can simplify error handling and make the code more concise.

Has this idea, or one like it, been proposed before?
No, this specific proposal has not been made before.

Who does this proposal help, and why?
This proposal benefits developers who are accustomed to more expressive error handling syntax in other languages. It aims to improve the readability and conciseness of error handling code.

What is the proposed change?
Introduce an advanced error handling syntax using the ?? operator, allowing for concise error handling in a single line or invoking custom error handling functions.

Please describe as precisely as possible the change to the language.
The proposed change introduces a new syntax ?? for handling errors in a more advanced and concise manner. It allows developers to handle errors directly inline or call custom error handling functions.

What would change in the language spec?
The language specification would need to be updated to include the new ?? operator and its behavior in the context of error handling.

Please also describe the change informally, as in a class teaching Go.
This change simplifies error handling by providing a concise syntax for handling errors inline or through custom error handling functions.

Is this change backward compatible?

  • No
  • Yes (Should be)

Show example code before and after the change.
Before:

func main() {
        bank := NewBank()
	err := bank.CreateAccount("123", 100.0)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
}

After:

func main() {
    bank := NewBank()
    createAccount, err := bank.CreateAccount("123", 100) ?? fmt.Errorf("CreateAccount failed. Error: %e", err)
}

//Advanced error handling
func main() {
    bank := NewBank()
    createAccount, err := bank.CreateAccount("123", 100) ?? {
        fmt.Errorf("CreateAccount failed. Error: %e", err)
                //etc
    }
}

//or even call some function
func main() {
    bank := NewBank()
    createAccount, err := bank.CreateAccount("123", 100) ?? someErrorHandler(err)
}

What is the cost of this proposal?
How many tools would be affected?

  • This proposal would impact code analysis tools, linters, and potentially code formatters.

What is the compile time cost?

  • Negligible, as it's a syntactic change.

What is the run time cost?

  • No runtime impact.

Can you describe a possible implementation?
An implementation would involve modifying the parser to recognize the new ?? syntax and updating the error handling logic accordingly.

How would the language spec change?
The language specification would need to include the definition and behavior of the ?? operator in the context of error handling.

Orthogonality: how does this change interact or overlap with existing features?
This change is orthogonal to existing features and does not overlap with them.

Is the goal of this change a performance improvement?

  • Yes
  • No

Does this affect error handling?

  • Yes
  • No

If so, how does this differ from previous error handling proposals?
This proposal focuses on enhancing the syntax for error handling, providing a more concise and expressive way to handle errors.

Is this about generics?

  • Yes
  • No

If so, how does this relate to the accepted design and other generics proposals?
N/A

@gopherbot gopherbot added this to the Proposal milestone Dec 12, 2023
@seankhliao
Copy link
Member

Duplicate of #37243

@seankhliao seankhliao marked this as a duplicate of #37243 Dec 12, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2023
@seankhliao seankhliao added LanguageChange Suggested changes to the Go language error-handling Language & library change proposals that are about error handling. v2 An incompatible library change labels Dec 12, 2023
@golang golang locked and limited conversation to collaborators Dec 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

3 participants