Standard library / errors

errors.Join()

Combines non-nil errors; returns nil if every argument is nil. Result wraps all supplied non-nil errors and joins their messages with newlines. Expected stdout: '"a\\nb" true\n'.

Formal Syntax & Structure

go syntaxgo
func Join(errs ...error) error

Parameters & Attributes

NameTypeDescription
errs...errorErrors to combine; nil entries are skipped.

Return Value

error

Combined error or nil if all operands are nil.

Working Example

Example Snippetgo
package main
import (  "errors"  "fmt")
func main() {  a, b := errors.New("a"), errors.New("b")  e := errors.Join(a, nil, b)  fmt.Printf("%q %v\n", e.Error(), errors.Is(e, b))}
Go Reference