Skip to content

Instantly share code, notes, and snippets.

@einthusan
Last active September 21, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einthusan/24e18f6359a31b3537815284cde0f6de to your computer and use it in GitHub Desktop.
Save einthusan/24e18f6359a31b3537815284cde0f6de to your computer and use it in GitHub Desktop.
Go2 Error Aware Keywords - `return`, `defer`, `if`, and `!=` and forcing the error object to be the last argument
// THE GOAL of my proposal is to keep Go2 easy to read, which was it's number 1 strength.
// I am an ordinary programmer and therefore I don't know how difficult it will be to implement my proposal.
// I hope that Go2 can remain simple and easy to read.
// The syntax for Go error handling will ultimately rely on the changes to Go error types/values.
// If Go error handling syntax and Go error type is treated as 2 seperate topics,
// then Go2 errors as a whole will produce a solution that is not user friendly.
// the 'return' keyword only returns if the error != nil
// this means Go2's 'return' keyword will be aware of error types
// same as writing: if err != nil { return err }
err := login()
return err
// a simple error can be named anything you like (e.g. err, e, e1, etc..)
e := login()
return e
// but should be careful when writing something like this as no error checking will be done at all
return login()
// only if an error != nil, return the error, but ofcourse don't forget to execute the defer function first
err := login()
return err defer cleanUpTask()
// a more complex example of the above, defer should also be a bit more intelligent
// NOTE: in the case of multiple return values, error must be the last return value
name, err := login()
return "", err defer cleanUpTask(), removeSession()
// a nested situation where the if keyword is error aware and able to extract a boolean value from error object
err := login()
if err {
e := oldLoginMethod()
return e
}
// return if error != nil but ONLY if its NOT an error type we intent to ignore
// in this situation, any error other than ErrWeDontCare & ErrWeDontMind will be treated as a valid error
err := login()
return err != example.ErrWeDontCare, example.ErrWeDontMind
// a complicated example
name, err := login()
return "", err != example.ErrWeDontCare defer cleanUpTask()
// a nested complicated example
name, err := login()
if err {
name, e: = oldLoginMethod()
return "", e != example.ErrWeDontCare, example.ErrWeDontMind defer cleanUpTask(), removeSession()
}
@kalexmills
Copy link

Seems to me that overloading return like this is confusing when in every other language, return exits the function.

@gmichelo
Copy link

gmichelo commented Sep 2, 2018

I think to agree with @kalexmills. I am not sure that overloading return operator is a wise choice. Even if this proposal leads to a more concise code, it does not seem easily understandable.

@einthusan
Copy link
Author

Yes I agree myself... this was a desperate attempt to prevent the current blizzare proposal for Go2. Honestly speaking, I learned to admire the current verbose Go1 way and I truly feel that there is NO REAL need to “fix” errors in Go. It’s no big deal, use snippets if it annoys you that much. Let’s keep Go2 the same as Go1 for errros please!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment