Skip to content

Instantly share code, notes, and snippets.

@8lall0
Last active October 13, 2018 09:25
Show Gist options
  • Save 8lall0/cb43e1fa4aae42bc709b138bda02284e to your computer and use it in GitHub Desktop.
Save 8lall0/cb43e1fa4aae42bc709b138bda02284e to your computer and use it in GitHub Desktop.
Feedback for Go2 error handling design

Honest to God, i agree with Alessandro Arzilli feedback.

Having a check statement near a function imho can make readability worse.

If we want to handle errors and avoid err != nil, i would like something like this:

From:

func Foo() (err error) {
    handle err { return fmt.Errorf("error: %v", err)  }
    check bar()
}

To:

func Foo() (err error) {
    // _check can be a special function that can easily be scoped
    _check = func(err) error {
      return fmt.Errorf("error: %v", err)
    }
    
    /* we put a question mark (or whatever) near err that MUST be an error type var.
       so you tell that after var assignation, it performs _check()*/
    fooVar, ?err := bar()
}

In this way:

  • we can scope the check function per-function or even per-package. If you need a custom handler for a single function, you simply assign it inside the function so it remains function-scoped.
  • if you need different check functions in the same function/package just overwrite it, so it doesn't break any flow
  • the actual err != nil is still valid and migration from go1 to this can sometimes be a simple deal of find/replace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment