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 1: Add error checking shorthand operator #58478

Closed
jeverz opened this issue Feb 11, 2023 · 1 comment
Closed

proposal: Go 1: Add error checking shorthand operator #58478

jeverz opened this issue Feb 11, 2023 · 1 comment

Comments

@jeverz
Copy link

jeverz commented Feb 11, 2023

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.

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
@gopherbot gopherbot added this to the Proposal milestone Feb 11, 2023
@seankhliao
Copy link
Member

Duplicate of #37243

@seankhliao seankhliao marked this as a duplicate of #37243 Feb 11, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 11, 2023
@golang golang locked and limited conversation to collaborators Feb 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants