stdlib

package module
v2.11.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2025 License: MIT Imports: 17 Imported by: 21

README

txsvc/stdlib

My "standard library" of go functions I always use in all kinds of projects and repos.

Documentation

Index

Constants

View Source
const (
	MsgMissingProvider         = "provider '%s' required"
	MsgUnsupportedProviderType = "unsupported provider type '%d'"
)
View Source
const (
	LOG_LEVEL = "LOG_LEVEL"
)
View Source
const (
	MinWordsInPassPhrase = 11
)

Variables

View Source
var (
	// ErrInvalidPassPhrase indicates that the pass phrase is too short
	ErrInvalidPassPhrase = errors.New("invalid pass phrase")
)

Functions

func Checksum

func Checksum(a string) string

Checksum creates a unique CRC32 checksum from a string

func CreateMnemonic added in v2.10.0

func CreateMnemonic(phrase string) (string, error)

func ElapsedTimeSince

func ElapsedTimeSince(t time.Time) int64

ElapsedTimeSince returns the difference between t and now.

func Exists

func Exists(env string) bool

Exists verifies that the environment variable 'env' is defined and returns a non-empty value.

func Fingerprint

func Fingerprint(a string) string

Fingerprint creates a unique ID (MD5) from a string

func GetBool added in v2.5.0

func GetBool(env string, def bool) bool

GetBool returns the environment variable ENV['env'] as boolean or def if 'env' is not set.

func GetInt

func GetInt(env string, def int64) int64

GetInt returns the environment variable ENV['env'] as an int64 or def if 'env' is not set.

func GetString

func GetString(env, def string) string

GetString returns the environment variable ENV['env'] or def if 'env' is not set. Note: def is only returned if the 'env' is not set, i.e. an EMPTY 'env' is still returned !

func IncT

func IncT(t int64, m int) int64

IncT increments a timstamp (in seconds) by m minutes.

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal marshals v into json bytes.

func MarshalToString

func MarshalToString(v interface{}) (string, error)

MarshalToString marshals v into a string.

func Nano

func Nano() int64

Nano returns the curent time in miliseconds UTC

func Now

func Now() int64

Now returns the curent time in seconds UTC

func RandPasswordString added in v2.10.0

func RandPasswordString(n int) string

func RandString

func RandString() string

RandString returns a random string.

func RandStringId

func RandStringId() string

RandStringId returns a random id string.

func RandStringN

func RandStringN(n int) string

RandString returns a random sting of lenght n based on a..zA..z0..9

func RandStringSimple added in v2.10.0

func RandStringSimple(n int) string

RandStringSimple returns a random sting of lenght n based on a..z0..9. This is basically the same as RandStringN but only case-insensitive.

func RandomToken

func RandomToken(prefix string) (string, error)

RandomToken generates a random token similar to a to a RFC 4122 UID

func Seed

func Seed(seed int64)

Seed sets the seed for the default random source. This affects all random string generation functions that don't use crypto/rand.

func SetLogLevel added in v2.5.0

func SetLogLevel()

SetLogLevel configures the global logging level based on the LOG_LEVEL environment variable. Supported values are: "trace", "debug", "info", "warn", "error". If the value is not recognized or not set, logging will be disabled.

func ShortUUID

func ShortUUID() (string, error)

ShortUUID returns a short (6 bytes) UID

func SimpleUUID

func SimpleUUID() (string, error)

SimpleUUID generates a random UUID according to RFC 4122, without any dashes

func ToHour

func ToHour(t int64) int

ToHour retuns the hour of the day for the timestamp, in local time

func ToHourUTC

func ToHourUTC(t int64) int

ToHourUTC retuns the hour of the day for the timestamp, UTC time

func ToUTC

func ToUTC(t int64) string

ToUTC converts a timestamp to UTC timestamp

func ToWeekday

func ToWeekday(t int64) int

ToWeekday retuns the day of the week for the timestamp

func UUID

func UUID() (string, error)

UUID generates a random UUID according to RFC 4122

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal unmarshals data bytes into v.

func UnmarshalFromReader

func UnmarshalFromReader(reader io.Reader, v interface{}) error

UnmarshalFromReader unmarshals v from reader.

func UnmarshalFromString

func UnmarshalFromString(str string, v interface{}) error

UnmarshalFromString unmarshals v from str.

Types

type AnyType

type AnyType = interface{}

AnyType can be used to hold any type.

type GenericProvider added in v2.10.0

type GenericProvider interface {
	Close() error
}

type InstanceProviderFunc added in v2.10.0

type InstanceProviderFunc func() interface{}

type PlaceholderType

type PlaceholderType = struct{}

PlaceholderType represents a placeholder type.

var Placeholder PlaceholderType

Placeholder is a placeholder object that can be used globally.

type Provider added in v2.10.0

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

func New added in v2.10.0

func New(opts ...ProviderConfig) (*Provider, error)

New creates a new provider instance and configures it with provider implementations

func (*Provider) Close added in v2.10.0

func (p *Provider) Close() bool

Close iterates over all registered ProviderConfigs and asks them to 'close'

func (*Provider) Find added in v2.10.0

func (p *Provider) Find(providerType ProviderType) (interface{}, bool)

Find returns the registered provider instance if defined. The bool flag is set to true if there is a provider and false otherwise.

func (*Provider) RegisterProviders added in v2.10.0

func (p *Provider) RegisterProviders(ignoreExists bool, opts ...ProviderConfig) error

RegisterProviders registers one or more providers. An existing provider will be overwritten if ignoreExists is true, otherwise the function returns an error.

type ProviderConfig added in v2.10.0

type ProviderConfig struct {
	ID   string
	Type ProviderType
	Impl InstanceProviderFunc
}

func WithProvider added in v2.10.0

func WithProvider(ID string, providerType ProviderType, impl InstanceProviderFunc) ProviderConfig

WithProvider returns a populated ProviderConfig struct.

type ProviderType added in v2.10.0

type ProviderType int

Directories

Path Synopsis
Most of the code is lifted from https://github.com/googleapis/google-api-go-client/blob/main/internal/settings.go
Most of the code is lifted from https://github.com/googleapis/google-api-go-client/blob/main/internal/settings.go

Jump to

Keyboard shortcuts

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