x

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2022 License: Apache-2.0 Imports: 19 Imported by: 2

README

Utility library for Go

go.zoe.im/x

Build Status Go Report Card codecov GoDoc On Sourcegraph PRs Welcome

Don't repeat yourself, every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DisableCache bool

DisableCache will disable caching of the home directory. Caching is enabled by default.

Functions

func Bytes2Str

func Bytes2Str(data []byte) string

Bytes2Str converts byte slice to a string without memory allocation. See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .

Note it may break if string and/or slice header will change in the future go versions.

Example

Output:

func GraceRun added in v0.2.6

func GraceRun(f func() error) error

GraceRun grace run a function

func GraceStart added in v0.2.6

func GraceStart(f func(ch GraceSignalChan) error) error

GraceStart start a grace function

func HomeDir added in v0.0.7

func HomeDir() (string, error)

HomeDir returns the home directory for the executing user.

This uses an OS-specific method for discovering the home directory. An error is returned if a home directory cannot be detected.

func IsInContainer added in v0.1.2

func IsInContainer(pid int) (bool, error)

IsInContainer checks if the service is being executed in docker or lxc container.

func LineWriter added in v0.3.17

func LineWriter(fn func(line []byte) error, ignoreEmpty bool) io.Writer

LineWriter return a writer with a line callback

func Reset added in v0.0.7

func Reset()

Reset clears the cache, forcing the next call to Dir to re-detect the home directory. This generally never has to be called, but can be useful in tests if you're modifying the home directory via the HOME env var or something.

func Str2Bytes

func Str2Bytes(str string) []byte

Str2Bytes converts string to a byte slice without memory allocation.

Note it may break if string and/or slice header will change in the future go versions.

Example

Output:

func WithHomeDir added in v0.0.7

func WithHomeDir(path string) (string, error)

WithHomeDir expands the path to include the home directory if the path is prefixed with `~`. If it isn't prefixed with `~`, the path is returned as-is.

Types

type Duration added in v0.3.19

type Duration time.Duration

Duration implement JSON marshall for time.Duration

func (Duration) MarshalJSON added in v0.3.19

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON implement MarshalJSON

func (*Duration) UnmarshalJSON added in v0.3.19

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON implement UnmarshalJSON

type Errors added in v0.2.1

type Errors []error

Errors contains multi error

func NewErrors added in v0.2.1

func NewErrors(errs ...error) Errors

NewErrors create errors

func (Errors) Add added in v0.2.1

func (es Errors) Add(ex ...error)

Add ad a new error to errors

func (Errors) Error added in v0.2.1

func (es Errors) Error() string

Error implements the error interface

func (Errors) IsNil added in v0.2.1

func (es Errors) IsNil() bool

IsNil return if contains no error

type GraceSignalChan added in v0.2.6

type GraceSignalChan chan struct{}

GraceSignalChan grace signal chan

type UUID added in v0.0.2

type UUID [16]byte

A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.

var (
	Nil UUID // empty UUID, all zeros
)

Well known namespace IDs and UUIDs

func MustUUID added in v0.0.2

func MustUUID(uuid UUID, err error) UUID

MustUUID returns uuid if err is nil and panics otherwise.

func NewRandomUUID added in v0.0.2

func NewRandomUUID() (UUID, error)

NewRandomUUID returns a Random (Version 4) UUID.

The strength of the UUIDs is based on the strength of the crypto/rand package.

A note about uniqueness derived from the UUID Wikipedia entry:

Randomly generated UUIDs have 122 random bits.  One's annual risk of being
hit by a meteorite is estimated to be one chance in 17 billion, that
means the probability is about 0.00000000006 (6 × 10−11),
equivalent to the odds of creating a few tens of trillions of UUIDs in a
year and having one duplicate.
Example

Output:

func NewRandomUUIDFromReader added in v0.0.2

func NewRandomUUIDFromReader(r io.Reader) (UUID, error)

NewRandomUUIDFromReader returns a Random (Version 4) UUID.

func NewUUID added in v0.0.2

func NewUUID() UUID

NewUUID creates a new random UUID or panics. New is equivalent to the expression

uuid.Must(uuid.NewRandom())
Example

Output:

func ParseUUID added in v0.0.2

func ParseUUID(s string) (UUID, error)

ParseUUID decodes s into a UUID or returns an error. Both the standard UUID forms of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the Microsoft encoding {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} and the raw hex encoding: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.

func (UUID) String added in v0.0.2

func (uuid UUID) String() string

String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx , or "" if uuid is invalid.

func (UUID) URN added in v0.0.2

func (uuid UUID) URN() string

URN returns the RFC 2141 URN form of uuid, urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.

type Value added in v0.3.13

type Value[T comparable] struct {
	// contains filtered or unexported fields
}

func NewValue added in v0.3.13

func NewValue[T comparable](v T) *Value[T]

NewValue create the value for expression

func Unwrap added in v0.5.0

func Unwrap[T comparable](mv T, err error) *Value[T]

Unwrap the value from (value, error) if err != nil, return v

func (*Value[T]) Or added in v0.3.13

func (v *Value[T]) Or(r T) *Value[T]

Or Value(a).Or(-1)

func (*Value[T]) Unwrap added in v0.3.16

func (v *Value[T]) Unwrap(mv T, err error) *Value[T]

Unwrap the value from (value, error) if err != nil, return v

func (*Value[T]) Value added in v0.5.0

func (v *Value[T]) Value() T

Directories

Path Synopsis
cli
config
Package config provider load configuration from file
Package config provider load configuration from file
examples
Package httputil contains lots of helper functions
Package httputil contains lots of helper functions
Package sh to run command programing
Package sh to run command programing
gen

Jump to

Keyboard shortcuts

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