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: exclamation assignment error handling #35644

Closed
afking opened this issue Nov 17, 2019 · 3 comments

Comments

@afking
Copy link

@afking afking commented Nov 17, 2019

Proposes an exclamation identifier ! similar to the blank identifier for assignment. A non zero value assigned to ! causes a run-time error. For example:

f, ! := os.Open(filename) // panics on error

Related to the try proposal this proposal aims to simplify error boilerplate. Unlike the try proposal it can be used for any type. The value v assigned to ! effectively calls:

if !reflect.ValueOf(v).IsZero() {
    panic(v)
}

I think this behaviour is similar to how type assertions panic on invalid non handled cases.
Maps can get similar behaviour with value, ! := myMap[key] which would panic on missing key.

As a larger example Go by Example: Reading Files could be written as:

package main

import (
    "bufio"
    "fmt"
    "io"
    "io/ioutil"
    "os"
)

func main() {
    dat, ! := ioutil.ReadFile("/tmp/dat")
    fmt.Print(string(dat))

    f, ! := os.Open("/tmp/dat")

    b1 := make([]byte, 5)
    n1, ! := f.Read(b1)
    fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1]))

    o2, ! := f.Seek(6, 0)
    b2 := make([]byte, 2)
    n2, ! := f.Read(b2)
    fmt.Printf("%d bytes @ %d: ", n2, o2)
    fmt.Printf("%v\n", string(b2[:n2]))

    o3, ! := f.Seek(6, 0)
    b3 := make([]byte, 2)
    n3, ! := io.ReadAtLeast(f, b3, 2)
    fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3))

    _, ! = f.Seek(0, 0)

    r4 := bufio.NewReader(f)
    b4, ! := r4.Peek(5)
    fmt.Printf("5 bytes: %s\n", string(b4))

    f.Close()
}

This removes the check function and calls to check. The panic stack trace would be simplified as the call to check is removed. I could see this being used for example code and simple scripts where panics are already used for error handling.

@gopherbot gopherbot added this to the Proposal milestone Nov 17, 2019
@gopherbot gopherbot added the Proposal label Nov 17, 2019
@ianlancetaylor ianlancetaylor changed the title proposal: Go 2 exclamation assignment error handling proposal: Go 2: exclamation assignment error handling Nov 17, 2019
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 17, 2019

Looks very similar to #22122.

@afking

This comment has been minimized.

Copy link
Author

@afking afking commented Nov 17, 2019

Thanks for linking.

@afking afking closed this Nov 17, 2019
@networkimprov

This comment has been minimized.

Copy link

@networkimprov networkimprov commented Nov 17, 2019

FYI, allowing a special character in place of an error variable is one of the most common suggestions to simplify error handling. I think I was the first to suggest it, but it's appeared in many flavors since. Some are on https://github.com/golang/go/wiki/Go2ErrorHandlingFeedback#recurring-themes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.