package errors
import "errors"
Package errors implements functions to manipulate errors.
Code:play
Output:Example¶
package main
import (
"fmt"
"time"
)
// MyError is an error implementation that includes a time and message.
type MyError struct {
When time.Time
What string
}
func (e MyError) Error() string {
return fmt.Sprintf("%v: %v", e.When, e.What)
}
func oops() error {
return MyError{
time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
"the file system has gone away",
}
}
func main() {
if err := oops(); err != nil {
fmt.Println(err)
}
}
1989-03-15 22:30:00 +0000 UTC: the file system has gone away
Index ¶
Examples ¶
Functions ¶
func New ¶
New returns an error that formats as the given text.
Code:play
Output: The fmt package's Errorf function lets us use the package's formatting
features to create descriptive error messages.
Code:play
Output:Example¶
package main
import (
"errors"
"fmt"
)
func main() {
err := errors.New("emit macho dwarf: elf header corrupted")
if err != nil {
fmt.Print(err)
}
}
emit macho dwarf: elf header corrupted
Example (Errorf)¶
package main
import (
"fmt"
)
func main() {
const name, id = "bimmler", 17
err := fmt.Errorf("user %q (id %d) not found", name, id)
if err != nil {
fmt.Print(err)
}
}
user "bimmler" (id 17) not found
Source Files ¶
- Version
- v1.8.4
- Published
- Oct 4, 2017
- Platform
- darwin/amd64
- Last checked
- 3 minutes ago –
Tools for package owners.