Go's approach to error handling is based on two ideas: Thus, errors are values, just like any other values returned by a function. You should therefore pay close attention to how you create and handle them. Some functions, like strings.Contains or strconv.FormatBool, can never fail. If a function can fail, it should return an additional value. If there's only one possible cause of failure, this value is a boolean: value, ok := cache.Lookup(key) if !ok { // key not found in cache } If the failure can have multiple causes, the return value is of type error: f, err := os.Open("/path/to/file") if err != nil { return nil, err } The simplest way to create an error is by using fmt.Errorf (or errors.New if no formatting is needed): // (from findlinks) if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("getting %s: %s", url, resp.Status) } But since the error type is an interface type error interface { Error() string } any type that implements a method with the signature Error() string is considered an error. Error-handling strategies So, how should you handle errors? Here are five strategies, roughly sorted by frequency of use. 1a) Propagate the error to the caller as-is// (from findlinks) resp, err := http.Get(url) if err != nil { return nil, err } 1b) Propagate the error to the caller with additional information// (from findlinks) doc, err := html.Parse(resp.Body) if err != nil { // html.Parse is unaware of the url so we add this information return nil, fmt.Errorf("parsing %s as HTML: %v", url, err) } When the error eventually reaches the program's main function, it should present a clear causal chain, similar to this NASA accident investigation: genesis: crashed: no parachute: G-switch failed: bad relay orientation 2) Retry if the error is transient// (from wait) func WaitForServer(url string) error { const timeout = 1 * time.Minute deadline := time.Now().Add(timeout) for tries := 0; time.Now().Before(deadline); tries++ { _, err := http.Head(url) if err == nil ...
First seen: 2025-06-26 04:21
Last seen: 2025-06-26 05:21