Help us shape the future of Go! Take the 2026 Go Developer Survey

multierr

package module
v0.0.0-...-24b0677 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

package multierr provides a simple way to collect errors and return them as a single error using errors.Join.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

type Collector interface {
	// Add appends an error to the collection.
	Add(error)
	// Err returns all collected errors as a single error, by calling errors.Join.
	// If no errors have been added, Err returns nil.
	Err() error
}

Collector collects multiple errors and returns them as a single error. Errors can be added by multierr.Collector.Add, and the final combined error is obtained by multierr.Collector.Err.

The resulting error supports errors.Is and errors.As checks for any of the collected errors.

func New

func New() Collector

New creates a new Collector.

This function is not concurrency-safe: if you plan to use the collector from multiple goroutines, consider wrapping calls with a mutex or using your own concurrency control.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/no-yan/multierr"
)

func main() {
	m := multierr.New()

	reasons := []string{"1st error", "2nd error"}
	for _, reason := range reasons {
		err := errors.New(reason)
		m.Add(err)
	}

	fmt.Println(m.Err())
}
Output:
1st error
2nd error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL