2 ms·
This isn't a situation where the choices are: 3 lines of IDE-Insert-Boilerplate, or Exceptions. I think Go could add syntax to keep its relatively unique form o
by 015a 4y ago
This isn't a situation where the choices are: 3 lines of IDE-Insert-Boilerplate, or Exceptions. I think Go could add syntax to keep its relatively unique form of error handling, while reducing code noise. The guard proposal is fine, but I'd prefer to see something like:
func MyFunc() error {
r := db.Get("12345") guard "error Get(12345)"
}
Syntactically really strange, but point being: if a function returns (any, ..., error); 'guard' is a keyword which eats the last 'error', optionally prefixes it with the string in that last keyword argument (standard golang error wrapping: "error Get(12345): {err.Error()}", and bubbles it up (along with zero or default values for the other arguments). It would also be nice, for more complex use-cases, if that last argument could be a mapping function as well, like:
func MyFunc() CustomError {
r := db.Get("12345") guard func(err error) CustomError {
return CustomError{baseErr: err, code: 500}
}
}
Though, I'd gladly settle to not have that, as it goes a little beyond the extreme syntactic simplicity I expect out of go.