Skip to content

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

@yangyile1990

Description

@yangyile1990

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
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageChangeSuggested changes to the Go languageProposalWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.error-handlingLanguage & library change proposals that are about error handling.v2An incompatible library change

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions