Skip to content

[Feature Request] Shorter error catching #18425

@graarh

Description

@graarh

Lets look at the common function

func MyFunc() (*SomeStruct, int, error) {
    data, err := callSomething()
    if err != nil {
        return nil, 0, err
    }
    return data, callSomethingOther(), nil
}

the

if err != nil {
   return <default type value>, <default type value>, err
}

is the most common error checking code. But it can be removed by adding syntax construction like

func MyFunc() (*SomeStruct, int, error) {
    data, <err> := callSomething()
    return data, callSomethingOther(), nil
}

that means if the err is not nil, exit from the function and return default type values except error value. It should not compile if more than one error type returned.

It is pretty clean for reading, and removes a lot of code lines with identical error checking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions