package errors

import "errors"

errors包实现了创建错误值的函数。

Example
package errors_test
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 Example() {
    if err := oops(); err != nil {
        fmt.Println(err)
    }
    // Output: 1989-03-15 22:30:00 +0000 UTC: the file system has gone away
}

Index

返回首页


  • func New(text string) error
  • Examples

    返回首页


  • New
  • New (Errorf)
  • package
  • func New

    func New(text string) error

    使用字符串创建一个错误,请类比fmt包的Errorf方法,差不多可以认为是New(fmt.Sprintf(...))。

    Example
    err := errors.New("emit macho dwarf: elf header corrupted")
    if err != nil {
        fmt.Print(err)
    }

    Output:

    emit macho dwarf: elf header corrupted
    
    Example (Errorf)
    const name, id = "bimmler", 17
    err := fmt.Errorf("user %q (id %d) not found", name, id)
    if err != nil {
        fmt.Print(err)
    }

    Output:

    user "bimmler" (id 17) not found