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: Go 2: simplify error handling and simplify conditions processing and "?" pipe operation symbol #39148

Closed
enddeadroyal opened this issue May 19, 2020 · 8 comments

Comments

@enddeadroyal
Copy link

@enddeadroyal enddeadroyal commented May 19, 2020

1.Simplify error handling:
for example (reference to rust)
old(go 1.12):

dat,err := ioutil.readFull("Go Programming.pdf")
if err != nil {
   log.error(err.Message())
   return err
}

OR

if dat,err := ioutil.readFull("Go Programming.pdf");err != nil {
   log.error(err.Message())
   return err
}

expect:

dat := ioutil.readFull("Go Programming.pdf") ? err =>  log.error(err.Message())

OR

dat := ioutil.readFull("Go Programming.pdf") ? err => {
     log.error(err.Message())
     return err
}

when err is nil, don't do err expression

2 Simplify Conditions Process
old(go 1.12):

dat,err := ioutil.readFull("Go Programming.pdf")
_,ok := err.(os.Err)
if ok {
 //do something
}
_,ok := err.(file.NotFindErr)
if ok {
 //do something
}

expect:

switch err => {
    os.Err, io.Err: =>    //do something
    file.NotFindErr:=> {
            //do something
     }
}

when 1 and 2 use at the same time

dat := ioutil.readFull("Go Programming.pdf") ? err => {
   switch err => {
     os.Err, io.Err:=>    //do something
     file.NotFindErr:=> {
            //do something
      }
    }
}

OR

dat := ioutil.readFull("Go Programming.pdf") ? switch err => {
    os.Err, io.Err:=>    //do something
    file.NotFindErr:=> {
            //do something
    }
}

"?" operation symbol is was used as a pipe which concat expressions
like as linux "|" operation symbol

netstat -an | grep 8080
@gopherbot gopherbot added this to the Proposal milestone May 19, 2020
@gopherbot gopherbot added the Proposal label May 19, 2020
@enddeadroyal enddeadroyal changed the title proposal: Go 2: Simplify error handling and Simplify Conditions Process proposal: Go 2: Simplify error handling and Simplify Conditions Process and "?" pipe operation symbol May 19, 2020
@ianlancetaylor ianlancetaylor changed the title proposal: Go 2: Simplify error handling and Simplify Conditions Process and "?" pipe operation symbol proposal: Go 2: simplify error handling and simplify conditions processing and "?" pipe operation symbol May 20, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 20, 2020

Has similarities to #33177, #32946, and likely others.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 20, 2020

The switch err => syntax seems quite similar to a type switch. I don't see the advantage to the new syntax.

The other syntax seems to change

if r, err := F(); err != nil {
    ...
}

to

r := F() ? err => {
    ...
}

If I'm counting right this replaces if, a comma, a semicolon, and != nil with ? and =>. This seems to save 6 characters, not counting whitespace. That doesn't seem like a huge benefit to me.

@enddeadroyal
Copy link
Author

@enddeadroyal enddeadroyal commented May 20, 2020

Thank you for answer

if r, err := F(); err != nil {
    ...
}

to

r := F() ? err => {
    ...
}

The scope of these two r is different
The first will make the nesting very serious

if r, err := F(); err != nil {
    ...
}else {
   if r, err := F(); err != nil {
    ...
   }else{
      if r, err := F(); err != nil {
         ...
      }else {
         ...
     }
   }
}

to

r := F() ? err => {
    ...
}
r := F() ? err => {
    ...
}
r := F() ? err => {
    ...
}
@enddeadroyal
Copy link
Author

@enddeadroyal enddeadroyal commented May 20, 2020

no need for “=>”

if r, err := F(); err != nil {
    ...
}

to

r := F() ? err {
    ....
}
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 20, 2020

Thanks for the correction on scoping. I don't think it affects the token count, though.

Eliminating => does save another two characters, so now overall it saves 8 characters.

@gocs
Copy link

@gocs gocs commented May 25, 2020

This looks like something like a ternary operator.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 26, 2020

This proposal introduces new syntax unlike anything else in Go. As discussed above, it doesn't appear to bring a significant savings in character count. It has similarities to other error handling proposals that have been declined.

For these reasons, this is a likely decline. Leaving open for four weeks for final comments.

-- for @golang/proposal-review

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jun 23, 2020

No further comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.