mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
Added the fmt.Errorf
function, and how to create error constants
parent
802ab6f283
commit
a0668e2630
16
Errors.md
16
Errors.md
@ -10,6 +10,14 @@ if failure {
|
||||
}
|
||||
```
|
||||
|
||||
or by using `fmt.Errorf`:
|
||||
|
||||
```go
|
||||
if failure {
|
||||
return fmt.Errorf("inverse tachyon pulse failed")
|
||||
}
|
||||
```
|
||||
|
||||
Error strings should not start with a capital letter because they'll often be prefixed before printing:
|
||||
|
||||
```go
|
||||
@ -35,6 +43,14 @@ func (p ParseError) Error() string {
|
||||
}
|
||||
```
|
||||
|
||||
If you want to create a constant string error, you can use a named type string:
|
||||
|
||||
```go
|
||||
type errorConst string
|
||||
|
||||
const ErrTooManyErrors errorConst = "too many errors found."
|
||||
```
|
||||
|
||||
Calling code would test for a special type of `error` by using a type switch:
|
||||
|
||||
```go
|
||||
|
Loading…
x
Reference in New Issue
Block a user