Comments (4)
The idea of const
errors is appealing! A tradeoff is that our exported
ErrCouldNotOpen
cannot have the type error
anymore.
// fails with: invalid constant type error
const ErrCouldNotOpen error = Error("foo")
A usability issue of this is that if we do err := ErrCouldNotOpen
, its type
is now Error
, and we can't assign another error
to it in the same scope.
err := ErrCouldNotOpen
...
// fails with: cannot assign error to err (type Error)
x, err := anotherOperation()
(https://play.golang.org/p/V2Gyd1mVkb9)
Admittedly, this isn't terribly inconvenient because most of the time we'll do
return ErrCouldNotOpen
where the return type is known to be error
, and in
the rare cases where we need it to be a variable, var err error = ..
will
suffice.
Also, to keep the exported API surface small, we'll likely want to use an
unexported type.
package foo
type fooError string
func (e fooError) Error() string { return string(e) }
const ErrCouldNotOpen = fooError("could not open")
I dislike the idea of exporting an entity with a type that is not meant to be
used directly.
I think we should chew on this a bit and weigh the usability and hygiene
tradeoffs.
from guide.
type Error
in
type Error string
const ErrCouldNotOpen = Error("could not open")
does not have Error() string
method implemented and so something as simple as
func f() error {
return ErrCouldNotOpen
}
won't compile (with cannot use ErrCouldNotOpen (type Error) as type error in return argument: Error does not implement error (missing Error method)
) unless
func (e Error) Error() string {
panic("implement me")
}
is added, which I personally think becomes a little bit too much code for a general case.
If there is a need for a full blown error type, then @abhinav's comment sums up pros/cons pretty well.
Something else to consider: if const Err...
becomes a recommendation then it would have to tagged with "for primitive types only" as const Err.. = errorStruct{...}
won't compile (which means we'll have both const & var error declarations...).
-1 from me on const Err...
recommendation.
from guide.
Agree with @abhinav, given the trade-offs, I don't think this is something we should do right now.
from guide.
FWIW, here's a simpler approach that opens the door to "effectively constant" sentinel error values:
var errCouldNotOpen = errors.New("could not open")
func ErrCouldNotOpen() error {
return errCouldNotOpen
}
When you think about it, this is essentially what context.Background
does.
from guide.
Related Issues (20)
- About updating uber-go-style-guide-kr HOT 2
- Possible issue with testing code HOT 1
- The style guide should include canonical example file(s) HOT 1
- Uber Go style should clarify ordering for type declarations and methods in files with multiple types HOT 8
- Releases for Watch Notifications HOT 2
- Proposal: Add Type Parameters ( Generics ) HOT 2
- Misleading formulation in "Receivers and Interfaces" HOT 1
- style: Linting the style.md with `markdownlint`
- Proposal: Note that VS Code and GoLand have provided “inlay hints” that can avoid naked parameters HOT 3
- proposal: table test guidance is too prescriptive HOT 3
- proposal: Avoid calling methods on map value, e.g. m[key].Method() HOT 1
- Proposal: prefer `%q` to format strings over `%s` HOT 1
- Question: how to use custom logger with the guide 'Exit-Once'
- Error Handling Approaches Discussion
- Uber Go Style Guide
- style.md#errors - Nesting can be removed in example of Error Types section
- Add information about github.com/pkg/errors HOT 2
- Update links to Go packages/wiki
- Translate this Guide to Deutsch HOT 2
- Verify Interface Compliance with Generic support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guide.