This proposal aims to reduce the boilerplate of basic error handling by introducing a new identifier.
We will use ?
in this proposal, which will behave as follows: when a error is assigned to ?
the function would returns immediately with the error received.
As for the other variables their values would be:
- if they are not named zero value
- the value assigned to the named variables otherwise
The use of ?
would allow to use +=
and similar operators on the other value, as if ?
was not there.
When ?
receives an error the last value of the named value would be returned.
From:
func foo(path string) (io.ReadCloser, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
if _, err = fmt.Fprint(f, "foo\n"); err != nil {
return nil, err
}
return f, nil
}
To:
func foo(path string) (io.ReadCloser, error) {
f, ? := os.Open()
_, ? = fmt.Fprint(f, "foo\n")
return f, nil
}
From:
func foo(r io.Reader) (n int, err error) {
b := make([]byte, 1024)
for {
a, err := r.Read()
n += a
if err != nil {
return n, err
}
}
}
To:
func foo(r io.Reader) (n int, err error) {
b := make([]byte, 1024)
for {
n, ? += r.Read()
}
}