deferred

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: MIT Imports: 3 Imported by: 0

README

deferred

idea:

This is a tiny library that contains helpers to close any closer that returns error with defer.

The general idea is simple: make it with one line.

example:

    ...
    resp, err := http.Get("http://example.com/")
    if err != nil {
        // Handle error
    }

    defer deferred.CloseOrLog(resp.Body, logger)
    ...

list of methods:

// pass logger you use in your code
- CloseOrLog(cl io.Closer, log logger)  
- CloseOrLogCtx(ctx context.Context, cl io.Closer, log logger)

// only if you don't mind using standard log
- CloseOrLogStd(cl io.Closer)              
- CloseOrLogStdCtx(ctx context.Context, cl io.Closer)

Documentation

Overview

Package deferred provides helpers to close `io.Closer“. This helpers can be used as one-liner with defer keyword to call `Close` method and check error if occurs.

example: ```go

...
resp, err := http.Get("http://example.com/")
if err != nil {
    // Handle error
}

defer deferred.CloseOrLog(resp.Body, logger)
...

```

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseOrLog

func CloseOrLog(cl io.Closer, log logger)

CloseOrLog tries to close `cl`. If error returned, log it. Supports loggers that have `Errorf` method. Like :

zap
logrus
apex/log
etc...

func CloseOrLogCtx

func CloseOrLogCtx(ctx context.Context, cl closerWithContext, log logger)

CloseOrLogCtx tries to close `cl`. If error returned, log it. Supports loggers that have `Errorf` method. Like :

zap
logrus
apex/log
etc...

func CloseOrLogStd

func CloseOrLogStd(cl io.Closer)

CloseOrLogStd tries to close `cl`. If error returned, log it with standard `log`.

func CloseOrLogStdCtx

func CloseOrLogStdCtx(ctx context.Context, cl closerWithContext)

CloseOrLogCtx tries to close `cl`. If error returned, log it with standard `log`.

Types

This section is empty.

Jump to

Keyboard shortcuts

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