Skip to content

proposal: "reflow" keyword for error handling #21146

Closed
@sebtzm

Description

@sebtzm

I would like to propose a new keyword "reflow".
This keyword acts as "return" only if the last value to return is not nil, otherwise it does nothing and execution of function goes on.
It can be very helpful to return values along with an error without having to write an if statement to check that the error is not nil every time.

It replaces a function like this:

func MyFunction() (data string, err error) {
	if data, err = doSomething(); err != nil {
		return data, err
	}
	
	...
	
	doAnotherThing(data)
	return data, nil
}

in a more concise way:

func MyFunction() (data string, err error) {
	data, err = doSomething()
	reflow data, err
	
	...
	
	doAnotherThing(data)
	return data, nil
}

Practically, the reflow keyword returns from function only if the last argument is not nil.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageChangeSuggested changes to the Go languageProposalerror-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