snapshot of https://github.com/uber-go/guide.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
52
src/error-name.md
Normal file
52
src/error-name.md
Normal file
@@ -0,0 +1,52 @@
|
||||
# Error Naming
|
||||
|
||||
For error values stored as global variables,
|
||||
use the prefix `Err` or `err` depending on whether they're exported.
|
||||
This guidance supersedes the [Prefix Unexported Globals with _](global-name.md).
|
||||
|
||||
```go
|
||||
var (
|
||||
// The following two errors are exported
|
||||
// so that users of this package can match them
|
||||
// with errors.Is.
|
||||
|
||||
ErrBrokenLink = errors.New("link is broken")
|
||||
ErrCouldNotOpen = errors.New("could not open")
|
||||
|
||||
// This error is not exported because
|
||||
// we don't want to make it part of our public API.
|
||||
// We may still use it inside the package
|
||||
// with errors.Is.
|
||||
|
||||
errNotFound = errors.New("not found")
|
||||
)
|
||||
```
|
||||
|
||||
For custom error types, use the suffix `Error` instead.
|
||||
|
||||
```go
|
||||
// Similarly, this error is exported
|
||||
// so that users of this package can match it
|
||||
// with errors.As.
|
||||
|
||||
type NotFoundError struct {
|
||||
File string
|
||||
}
|
||||
|
||||
func (e *NotFoundError) Error() string {
|
||||
return fmt.Sprintf("file %q not found", e.File)
|
||||
}
|
||||
|
||||
// And this error is not exported because
|
||||
// we don't want to make it part of the public API.
|
||||
// We can still use it inside the package
|
||||
// with errors.As.
|
||||
|
||||
type resolveError struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (e *resolveError) Error() string {
|
||||
return fmt.Sprintf("resolve %q", e.Path)
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user