errors

Package errors implements functions to manipulate errors.

Index

Functions

func New

1func New(text string) error

New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical.

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

Output

emit macho dwarf: elf header corrupted

The fmt package's Errorf function lets us use the package's formatting features to create descriptive error messages.

1const name, id = "bimmler", 17
2err := fmt.Errorf("user %q (id %d) not found", name, id)
3if err != nil {
4	fmt.Print(err)
5}

Output

user "bimmler" (id 17) not found

© Matthias Hochgatterer – MastodonGithubRésumé