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: add ? or ?() to the golang to handle the err return #66309

Closed
yangyile1990 opened this issue Mar 14, 2024 · 4 comments
Closed
Labels
error-handling Language & library change proposals that are about error handling. LanguageChange Proposal v2 A language change or 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 v2 A language change or 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
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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error-handling Language & library change proposals that are about error handling. LanguageChange Proposal v2 A language change or incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
Status: Incoming
Development

No branches or pull requests

5 participants