Skip to content

Proposal: Introduce if err ... Syntax to Simplify Go Error Checks and Returns #73897

Closed as not planned
@wzzhu

Description

@wzzhu

Proposal Details

Proposal: Introduce if err ... shorthand statement for concise error handling

Summary

This proposal introduces a new shorthand syntax in Go to simplify the common idiom of error checking and returning zero values alongside the error. The new syntax:

if err ...

is equivalent to:

if err != nil {
    return <zero values for all other return parameters>, err
}

This reduces boilerplate, improves code readability, and enforces consistent error propagation without requiring explicit zero value returns.


Motivation

Go’s explicit error handling with:

if err != nil {
    return 0, "", err
}

is clear and idiomatic but often verbose and repetitive, especially in functions with multiple return values. Developers frequently write boilerplate code returning zero values for all non-error returns alongside the error.

This verbosity:

  • Adds cognitive load,
  • Increases chance of mistakes (e.g., incorrect zero values),
  • Clutters business logic with repetitive code.

The proposed shorthand if err ...:

  • Eliminates the need to manually specify zero values,
  • Maintains explicit error checking semantics,
  • Improves code clarity and conciseness.

Detailed Design

Syntax

The new statement is:

if err ...

where err is a variable of type error.

Semantics

  • if err ... is equivalent to:
if err != nil {
    return <zero values for all other return parameters>, err
}
  • The compiler automatically infers zero values for all return parameters except the error, which is returned as-is.
  • This statement can only appear inside functions with multiple return values where the last return value is of type error.
  • The zero values correspond to the declared function return types.

Examples

Before:

func readFile() ([]byte, error) {
    data, err := ioutil.ReadFile("file.txt")
    if err != nil {
        return nil, err
    }
    return data, nil
}

After:

func readFile() ([]byte, error) {
    data, err := ioutil.ReadFile("file.txt")
    if err ...  // implicitly returns nil, err if err != nil
    return data, nil
}

For multiple return values:

func parseData() (int, string, error) {
    n, s, err := someParse()
    if err ...  // returns 0, "", err if err != nil
    return n, s, nil
}

Implementation Notes

  • The compiler must verify that:
    • The function’s last return type is error.
    • The variable after if is of type error.
    • The function has at least one non-error return value.
  • The compiler generates zero values for all non-error return parameters automatically.
  • The feature is purely syntactic sugar; no runtime overhead.

Compatibility

  • Backward compatible: existing code remains valid.
  • The new syntax is an additive feature and does not conflict with existing syntax.
  • Tools (linters, formatters) will need updates to support the new syntax.

Alternatives Considered

  • Using named return values with naked returns reduces zero value verbosity but can reduce clarity and is error-prone.
  • Helper functions to return zero values add boilerplate and reduce flexibility.
  • Exception-like error handling is against Go’s design philosophy.

Benefits

  • Reduces boilerplate and repetitive code.
  • Improves readability and maintainability.
  • Encourages consistent error handling patterns.

Drawbacks

  • Adds a new syntax to the language, increasing complexity slightly.
  • May encourage implicit returns, which some may argue reduces explicitness.

Future Work

  • Extend shorthand to support custom zero-value overrides.
  • Support similar shorthand for other common error handling patterns.

Conclusion

The if err ... shorthand statement offers a clean, concise, and idiomatic way to handle errors in Go by implicitly returning zero values alongside errors, reducing boilerplate and improving developer experience without compromising clarity or performance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LanguageChangeSuggested changes to the Go languageLanguageProposalIssues describing a requested change to the Go language specification.Proposalerror-handlingLanguage & library change proposals that are about error handling.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions