util

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2017 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package util contains useful components for simplifying Go code.

The package contains common error types (errors.go) and functions for converting arrays to pointers.

Index

Constants

This section is empty.

Variables

View Source
var TestEnvVarName = "TEST_FILESYSTEM_ROOT"

TestEnvVarName is the name on an environment variable that should be set to an empty mountpoint. This is only used for integration tests.

Functions

func CheckValidLength

func CheckValidLength(expected, actual int) error

CheckValidLength returns an invalid length error if expected != actual

func Index

func Index(inVal int64, inArray []int64) (index int, ok bool)

Index returns the first index i such that inVal == inArray[i]. ok is true if we find a match, false otherwise.

func Lookup

func Lookup(inVal int64, inArray, outArray []int64) (outVal int64, ok bool)

Lookup finds inVal in inArray and returns the corresponding element in outArray. Specifically, if inVal == inArray[i], outVal == outArray[i]. ok is true if we find a match, false otherwise.

func MinInt

func MinInt(a, b int) int

MinInt returns the lesser of a and b.

func MinInt64

func MinInt64(a, b int64) int64

MinInt64 returns the lesser of a and b.

func NeverError

func NeverError(err error)

NeverError panics if a non-nil error is passed in. It should be used to check for logic errors, not to handle recoverable errors.

func Ptr

func Ptr(slice []byte) unsafe.Pointer

Ptr converts an Go byte array to a pointer to the start of the array.

func TestPath

func TestPath() (string, error)

TestPath returns a the path specified by TestEnvVarName. The function panics if the environment variable is not set. This function is only used for integration tests.

Types

type ErrReader

type ErrReader struct {
	// contains filtered or unexported fields
}

ErrReader wraps an io.Reader, passing along calls to Read() until a read fails. Then, the error is stored, and all subsequent calls to Read() do nothing. This allows you to write code which has many subsequent reads and do all of the error checking at the end. For example:

r := NewErrReader(reader)
r.Read(foo)
r.Read(bar)
r.Read(baz)
if r.Err() != nil {
	// Handle error
}

Taken from https://blog.golang.org/errors-are-values by Rob Pike.

func NewErrReader

func NewErrReader(reader io.Reader) *ErrReader

NewErrReader creates an ErrReader which wraps the provided reader.

func (*ErrReader) Err

func (e *ErrReader) Err() error

Err returns the first encountered err (or nil if no errors occurred).

func (*ErrReader) Read

func (e *ErrReader) Read(p []byte) (n int, err error)

Read runs ReadFull on the wrapped reader if no errors have occurred. Otherwise, the previous error is just returned and no reads are attempted.

type ErrWriter

type ErrWriter struct {
	// contains filtered or unexported fields
}

ErrWriter works exactly like ErrReader, except with io.Writer.

func NewErrWriter

func NewErrWriter(writer io.Writer) *ErrWriter

NewErrWriter creates an ErrWriter which wraps the provided reader.

func (*ErrWriter) Err

func (e *ErrWriter) Err() error

Err returns the first encountered err (or nil if no errors occurred).

func (*ErrWriter) Write

func (e *ErrWriter) Write(p []byte) (n int, err error)

Write runs the wrapped writer's Write if no errors have occurred. Otherwise, the previous error is just returned and no writes are attempted.

type SystemError

type SystemError string

SystemError is an error that should indicate something has gone wrong in the underlying system (syscall failure, bad ioctl, etc...).

func (SystemError) Error

func (s SystemError) Error() string

Jump to

Keyboard shortcuts

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