package cleanup
import "errors"
func CloseAll(closers ...func() error) error {
var errs []error
for _, c := range closers {
if c == nil {
continue
}
if err := c(); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
Cleanup paths are easy to under-test, and it’s common to ignore close errors because you only have one return value. With Go 1.20+, errors.Join gives you a clean way to accumulate multiple cleanup errors and return them as a single error value. This is especially useful when you have several resources to close (rows, files, temp dirs) and you don’t want the first failure to hide the second. In production, this matters for batch jobs and import pipelines where a failed flush or close can mean data loss. The helper below takes a list of func() error closers and joins any errors it sees. It stays readable and keeps cleanup logic centralized. Combine this with structured logs and you’ll actually notice close failures instead of silently discarding them.
Related snips
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
export interface RetryOptions {
retries: number;
baseMs: number;
maxMs: number;
signal?: AbortSignal;
onRetry?: (attempt: number, delay: number, err: unknown) => void;
Exponential backoff with jitter for retries
package dbutil
import (
"context"
"github.com/jackc/pgconn"
Retry Postgres serialization failures with bounded attempts
package files
import (
"context"
"time"
Presigned S3 upload URLs (AWS SDK v2)
package deps
import (
"net"
"net/http"
"time"
HTTP client tuned for production: timeouts, transport, and connection reuse
package api
import (
"io"
"net/http"
"os"
Safe multipart uploads using temp files (bounded memory)
Share this code
Here's the card — post it anywhere.