Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: gofmt semicolon before if-return statements #27450

Closed
jozef-slezak opened this issue Sep 2, 2018 · 3 comments
Closed

proposal: gofmt semicolon before if-return statements #27450

jozef-slezak opened this issue Sep 2, 2018 · 3 comments

Comments

@jozef-slezak
Copy link

Let's allow a semicolon before short inlined if-return to cleanup error handling code - like this:

err := funcX(); if err != nil { return err }
err = funcY(); if err != nil { return err }

Nowdays gofmt replaces the code like this:

err := funcX()
if err != nil {
   return err
}
err = funcY()
if err != nil {
   return err
}

Pros:

  • easy to understand
  • cleans up repeated if err code (demonstrated on example above)
  • simple change in gofmt

Cons:

This issue is related to: #27135 and also error handling design of GO2 (I have already posted some feedback in https://gist.github.com/jozef-slezak/93a7d9d3d18d3fce3f8c3990c031f8d0 but I think we should consider this simple and readable gofmt change even in GO1).

@gopherbot gopherbot added this to the Proposal milestone Sep 2, 2018
@bronze1man
Copy link
Contributor

bronze1man commented Sep 3, 2018

It is strange that I think One statement one line is easy to understand/read to me.

I think putting a lot of stuff into one line, just make the code difficult to read.

So I proposal another syntax:

func funcX() (error){
  return nil
}
func funcY() (int,errror){
  return 0,nil
}
func aaa() (int,error){
  try funcX();
  a := try funcY();
  return a,nil
}

The try just check the last function return parameters is error type ,if that err value is not nil, return the parent function with that error value at that line and do not run code after that line.

Also see https://ziglang.org/documentation/master/#try

@networkimprov
Copy link

@rsc
Copy link
Contributor

rsc commented Sep 19, 2018

Duplicate of #27135, or at least same answer.

@rsc rsc closed this as completed Sep 19, 2018
@golang golang locked and limited conversation to collaborators Sep 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants