iocloser

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2022 License: Apache-2.0 Imports: 1 Imported by: 8

README

iocloser

This package intends to provide an easy and concise of closing io.Closers.

Typically you will find close that looks like:

defer reader.Close()

Your lint checkers will likely start complaining that you didn't properly handle the error. An error that you really don't care about.

So you might decide to do:

defer func() {
	_ = reader.Close()
  }()

This will make some linters happy but others will not let you get off so easily.

So instead of writing the following (arguably even uglier code):

defer func() {
	err := reader.Close()
	if err != nil {
		log.Printf("error was: %v", err)
	}
  }()

This package allows you to get it back to one line:

defer iocloser.Close(reader, log.Printf)

Or just

defer iocloser.Close(reader)

For usage examples please refer here

Documentation

Overview

Package iocloser please refer to README.md

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(reader io.Closer, logger ...Logger)

Close will close the supplied ReadCloser and optionally log

Example
package main

import (
	"fmt"

	"github.com/corsc/go-commons/iocloser"
)

func main() {
	myCloser := &myCloser{}

	iocloser.Close(myCloser)

	fmt.Printf("wasCalled: %v", myCloser.wasCalled)
}

type myCloser struct {
	wasCalled bool
	err       error
}

// Close implements io.Closer
func (m *myCloser) Close() error {
	m.wasCalled = true
	return m.err
}
Output:

wasCalled: true

Types

type Logger

type Logger func(template string, args ...interface{})

Logger defines the logging function for this package

Jump to

Keyboard shortcuts

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