Skip to content

proposal: Go 2: add ? or ?() to the golang to handle the err return #66309

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
yangyile1990 opened this issue Mar 14, 2024 · 4 comments
Closed
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 WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@yangyile1990
Copy link

Proposal Details

func exampleFunction() (any, error){
    res, err := run()
    err ? return nil, err

    num, err := calc()
    err ? return nil, errors.Errorf("calc is wrong. error is:%v", err)

    // if the err need more than one line to handle
    val, err := get()
    if err != nil {
        do1()
        do2()
        return nil, err
    }

    //or can use this( may be no need to add this):
    err ? {
        do1()
        do2()
        return nil, err
    }
}

The ? can be ? or ?-> or ?! or some other symbol not using in golang before.

The ? can means if anything != nil ...

you can also add ?() to handle err return.

for example:

func exampleFunction() (any, error){
    res, err := run()
    err ?(nil, err) // means if err != nil return nil, err

    num, err := calc()
    err ?(nil, errors.Errorf("calc is wrong. error is:%v", err)) // means if err != nil return nil, errors.Errorf(balabala)

    // if the err need more than one line to handle
    val, err := get()
    if err != nil {
        do1()
        do2()
        return nil, err
    }

    //or can use this( may be no need to add this):
    err ? {
        do1()
        do2()
        return nil, err
    }
}
@gopherbot gopherbot added this to the Proposal milestone Mar 14, 2024
@seankhliao
Copy link
Member

Please fill out https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing language changes

@seankhliao seankhliao added LanguageChange Suggested changes to the Go language v2 An incompatible library change error-handling Language & library change proposals that are about error handling. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Mar 14, 2024
@adonovan adonovan moved this to Incoming in Proposals Mar 14, 2024
@adonovan
Copy link
Member

Also, there have been a great many proposals attempting to improve error handling. Any new one should acknowledge the challenges and lessons of those proposals to avoid merely re-litigating.

@chad-bekmezian-snap
Copy link

This does nothing to improve error handling either. It just provides short hand to ignore an error

@seankhliao seankhliao changed the title proposal: add ? or ?() to the golang to handle the err return proposal: Go 2: add ? or ?() to the golang to handle the err return Mar 14, 2024
@gopherbot
Copy link
Contributor

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@gopherbot gopherbot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2024
@golang golang locked and limited conversation to collaborators Apr 14, 2025
@aclements aclements removed this from Proposals Apr 16, 2025
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 WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants