You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm a graduate student of Go programming. I've experience in Python, C, C++, C# and Rust.
Related proposals
While there have been proposals for new keywords to handle error checking, none I've found have been so simple.
Proposal
Many people find the error checking in Go tedious. I propose to add error checking shorthand to the language to reduce amount of code required and to increase readability of the code. It will also reduce copy and paste error checking and potential mistakes introduced by doing so. The change would be backwards compatible with Go by adding an optional operator ??. I've used the ?? operator here but a keyword would work just as well.
r1, _ = someFunc() ?? // This is a quick error return
r2, err = someFunc() ?? { // This is a quick error check
// Error handling code
}
Here is a simple example.
func doSomething() (int, error) {
// snip
}
func retErr() (int, error) {
// This is a quick error return. It will find the first returned error and check if it's not nill.
// If so the function will return default values and copy the error into the return value.
// Whether the error value is discarded or not has no effect on this new feature.
r1, _ := doSomething() ??
// This is a quick error check. It will find the first returned error and check if it's not nill.
// If so it will execute what's inside the braces.
r2, err := doSomething() ?? {
log.Print("uh-oh");
return 0, err;
}
return r1 + r2;
}
func retNoErr() int {
// Quick error return will not compile because the function doesn't return an error
// r := doSomething() ??
// So we have to use the Quick error check instead
r, _ := doSomething() ?? {
// We could panic here
return -1;
}
return r
}
Costs
This would make code easier to read and write.
It would be an additional thing to learn about the language but by no means difficult.
The compiler and gofmt would be affected and I'm unsure of how many other tools.
The compile time would be increased
The run time would remain the same
The text was updated successfully, but these errors were encountered:
Author background
I'm a graduate student of Go programming. I've experience in Python, C, C++, C# and Rust.
Related proposals
While there have been proposals for new keywords to handle error checking, none I've found have been so simple.
Proposal
Many people find the error checking in Go tedious. I propose to add error checking shorthand to the language to reduce amount of code required and to increase readability of the code. It will also reduce copy and paste error checking and potential mistakes introduced by doing so. The change would be backwards compatible with Go by adding an optional operator ??. I've used the ?? operator here but a keyword would work just as well.
Here is a simple example.
Costs
The text was updated successfully, but these errors were encountered: