Error handling

Custom error types

An error type implements Error() string. Typed errors can carry structured information rather than forcing consumers to parse text; choose consistent pointer or value receivers and use errors.As for extraction. Example output: 'true 7'.

Formal Syntax & Structure

go syntaxgo
func (e *Failure) Error() string

Working Example

Example Snippetgo
package main
import "fmt"import "errors"
type Failure struct{ Code int }
func (e *Failure) Error() string { return fmt.Sprintf("failure %d", e.Code) }func main() {  var e error = &Failure{Code: 7}  var f *Failure  fmt.Println(errors.As(e, &f), f.Code)}
Go Reference