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
emcfarlane opened this issue Nov 17, 2019 · 3 comments
Closed

proposal: Go 2: exclamation assignment error handling #35644

emcfarlane opened this issue Nov 17, 2019 · 3 comments
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@emcfarlane
Copy link

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
@ianlancetaylor ianlancetaylor changed the title proposal: Go 2 exclamation assignment error handling proposal: Go 2: exclamation assignment error handling Nov 17, 2019
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange error-handling Language & library change proposals that are about error handling. labels Nov 17, 2019
@ianlancetaylor
Copy link
Contributor

Looks very similar to #22122.

@emcfarlane
Copy link
Author

Thanks for linking.

@networkimprov
Copy link

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

@golang golang locked and limited conversation to collaborators Nov 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

4 participants