ut

package module
v0.0.0-...-2f614c7 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2021 License: MIT Imports: 2 Imported by: 1

README

ut package

The ut package exposes some utilities the subpackage utio exposes some IO related utilities. The package is licensed under MIT. Currently this package is still under development and thus the API may break.

API

Packages:

ut package

Deopter

Deopter may be used an action may or may not be executed. Particularly useful in combination with defer.

Example:

function Open(p string) (*File, error) {
    var f, _ = os.Open(p)
    var d = NewDeopter(f.Close)
    defer d.Invoke()

    var err error
    // code that might exit early    
    if nil != err {
        return nil, err
    }

    // successful code path
    d.Deopt()
    return f
}

Errors

An Errors struct that represents multiple errors (but still adheres to the error interface)

type Errors struct {
	Errs []error
}

Combine errors using ErrCombine when you want to combine multiple errors as one. Makes use of the Errors interface when there is more than one error.

var err error
err = ErrCombine(err, err2)
utio package

Encoder/decoder interfaces and two implementations:

  • JSON
  • PNG
type Decoder interface {
    Decode(io.Reader) error
}

type Encoder interface {
    Encode(io.Writer) error
}

Utility methods for reading/writing (with or without encoding) files and strings.

// File methods
func DecodeFile(string, Decoder) error
func ReadFile(string, ReadFunc) error

func EncodeFile(string, Encoder) error
func WriteFile(string, WriteFunc) error

// String methods
func DecodeFromString(string, Decoder) error
func ReadFromString(string, ReadFunc) error

func EncodeToString(Encoder) (string, error)
func WriteToString(WriteFunc) (string, error)

Utility methods related to the os/file system.

func Home() (string, error) // returns the user directory
func PathExists(string) bool
func PathDoesNotExist(string) bool

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrCombine

func ErrCombine(errs ...error) error

ErrCombine combines one or more errors, nil entries are omitted and nil is returned if all given errors are nil. The first argument is expanded if it satisfies the Errors interface. In case of aggregation of errors the return error will satisfy the Errors interface.

Types

type DeoptFunc

type DeoptFunc func() error

DeoptFunc describes the action signature for the Deopter.

type Deopter

type Deopter interface {
	Add(...DeoptFunc)
	Deopt()
	Invoke() error
}

Deopter describes an action that can be opted-out. This can be used in combination with defer to clean up

func NewDeopter

func NewDeopter(fn ...DeoptFunc) Deopter

NewDeopter creates a deopter for the functions supplied.

type Errors

type Errors struct {
	Errs []error
}

Errors exposes slice of errors.

func (Errors) Error

func (e Errors) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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