std

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const EmailLengthMax = 255
View Source
const EmailLengthMin = 8 // Min 2 chars for each part + "@" and "."

Variables

View Source
var EmailNil = Email{}

Functions

func ChanReadAll

func ChanReadAll[T any](ch <-chan T) []T

func GobDecodeBytes added in v0.6.0

func GobDecodeBytes[T any](data []byte) (T, error)

GobDecodeBytes

See bytes.NewBuffer() See gob.NewDecoder().Decode().

func GobEncodeBytes added in v0.6.0

func GobEncodeBytes(v any) ([]byte, error)

GobEncodeBytes

See bytes.NewBuffer() See gob.NewEncoder().Encode().

func MaskStr added in v0.2.0

func MaskStr(s string) string

MaskStr

Examples:

  • "" --> ""
  • "s" --> "*"
  • "Secret-codE" --> "***********"

func MaskStrNotFirst added in v0.2.0

func MaskStrNotFirst(s string) string

MaskStrNotFirst

Examples:

  • "" --> ""
  • "s" --> "s"
  • "Secret-codE" --> "S**********"

func MaskStrNotFirstLast added in v0.2.0

func MaskStrNotFirstLast(s string) string

MaskStrNotFirstLast

Examples:

  • "" --> ""
  • "s" --> "s"
  • "se" --> "se"
  • "Secret-codE" --> "S*********E"

func Must added in v0.5.0

func Must[T any](v T, err error) T

func PtrVal added in v0.6.0

func PtrVal[T any](p *T, nilVal T) T

func Ternary

func Ternary[T any](condition bool, rTrue T, rFalse T) T

Types

type Email

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

func EmailFromString

func EmailFromString(value string) (Email, error)

EmailFromString

Errors:

  • ErrorValidation

func EmailFromStringMust

func EmailFromStringMust(value string) Email

func (Email) Domain

func (e Email) Domain() string

func (Email) IsNil

func (e Email) IsNil() bool

func (Email) Name

func (e Email) Name() string

func (Email) String

func (e Email) String() string

type ErrorAlreadyDone

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

func NewErrorAlreadyDoneFf

func NewErrorAlreadyDoneFf(msg string, msgArgs ...any) ErrorAlreadyDone

NewErrorAlreadyDoneFf

Panics in case of empty arguments:

  • msg

func (ErrorAlreadyDone) Error

func (e ErrorAlreadyDone) Error() string

type ErrorCancelled added in v0.6.0

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

func NewErrorCancelledFf added in v0.6.0

func NewErrorCancelledFf(msg string, msgArgs ...any) ErrorCancelled

NewErrorCancelledFf

Panics in case of empty arguments:

  • msg

func (ErrorCancelled) Error added in v0.6.0

func (e ErrorCancelled) Error() string

type ErrorNotFound

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

func NewErrorNotFoundFf

func NewErrorNotFoundFf(msg string, msgArgs ...any) ErrorNotFound

NewErrorNotFoundFf

Panics in case of empty arguments:

  • msg

func (ErrorNotFound) Error

func (e ErrorNotFound) Error() string

type ErrorRuntime

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

ErrorRuntime

Client code should be able to distinguish business-logic errors from others. Moreover, each type of business-logic error may be handled differently. Runtime errors are typically handled uniformly (e.g: logged, reported, translated into an HTTP 500 response).

Go does not allow us to mark business-logic errors and then check that mark in the client code in a simple way, like some other OOP languages do (e.g., through inheritance and try‑catch statements), but it allows to get the same result in another way -- by wrapping all non-business-logic errors into a specific error type.

I.e:

try { ... }
catch (BusinessLogicException) { /* ... */ }
catch (Exception) { /* any other error -- in this case non-business-logic */ }

-->

switch err.(type) {
case nil:
case std.ErrorRuntime: /* any other error -- in this case non-business-logic */
default: /* ... */
}

--

For some cases it may be important to distinguish temporary runtime errors from permanent (e.g. for retry strategies). ErrorRuntime has a special flag for this -- IsTemporary() / IsPermanent().

--

Usage example:

package example

func SomeFunc() error {
	v, err := ...
	if err != nil {
		return std.ErrToRuntime(err, "example", "SomeFunc")
	}
	...
}

type MyType struct { ... }

func (m *MyType) SomeMethod() (..., error) {
	...
	if err != nil {
		return std.ErrToRuntime(err, m, "SomeMethod")
	}
	...
}

func ErrToRuntime added in v0.6.0

func ErrToRuntime(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime

ErrToRuntime

Wraps provided error into ErrorRuntime.

Adds this kind of prefix to the error to imitate stack trace: "{{`methodOwner`}}.{{`methodName`}}/{{joined by "/" elements of the `methodInfo`}}".

If `err` is a temporary ErrorRuntime (wrapped or not -- errors.As() is used as a checker), result error also will be marked as temporary, otherwise -- permanent. To mark result error by force use ErrToRuntimePermanent() or ErrToRuntimeTemporary().

Panics in case of empty arguments:

  • err
  • methodOwner
  • methodName

Arguments:

  • `methodOwner` -- string or any method owner instance. {{`methodOwner`}} will be replaced by the string value or method owner instance type accordingly. String value is allowed, for example, to accept package names, when a regular function is called, not a method.
  • `methodName` -- name of the method / function, that was called.
  • `methodInfo` -- any additional info.

func ErrToRuntimePermanent added in v0.6.0

func ErrToRuntimePermanent(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime

ErrToRuntimePermanent

Same as ErrToRuntime, but marks result error as permanent, even if source error was temporary.

func ErrToRuntimeTemporary added in v0.6.0

func ErrToRuntimeTemporary(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime

ErrToRuntimeTemporary

Same as ErrToRuntime, but marks result error as temporary, even if source error was permanent.

func NewErrorRuntimeFf

func NewErrorRuntimeFf(msg string, msgArgs ...any) ErrorRuntime

NewErrorRuntimeFf

DEPRECATED: use NewErrorRuntimePermanentFf() or NewErrorRuntimeTemporaryFf() instead.

func NewErrorRuntimePermanentFf added in v0.6.0

func NewErrorRuntimePermanentFf(msg string, msgArgs ...any) ErrorRuntime

NewErrorRuntimePermanentFf

See ErrorRuntime for details.

func NewErrorRuntimeTemporaryFf added in v0.6.0

func NewErrorRuntimeTemporaryFf(msg string, msgArgs ...any) ErrorRuntime

NewErrorRuntimeTemporaryFf

See ErrorRuntime for details.

func WrapErrorToRuntime

func WrapErrorToRuntime(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime

WrapErrorToRuntime

DEPRECATED: use ErrToRuntime() instead.

func (ErrorRuntime) Error

func (e ErrorRuntime) Error() string

func (ErrorRuntime) IsPermanent added in v0.6.0

func (e ErrorRuntime) IsPermanent() bool

IsPermanent

Same as "not IsTemporary()".

See NewErrorRuntimePermanentFf(). See ErrToRuntimePermanent().

func (ErrorRuntime) IsTemporary added in v0.6.0

func (e ErrorRuntime) IsTemporary() bool

IsTemporary

See NewErrorRuntimeTemporaryFf(). See ErrToRuntimeTemporary().

func (ErrorRuntime) Unwrap

func (e ErrorRuntime) Unwrap() error

type ErrorUnprocessable

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

func NewErrorUnprocessableFf

func NewErrorUnprocessableFf(msg string, msgArgs ...any) ErrorUnprocessable

NewErrorUnprocessableFf

Panics in case of empty arguments:

  • msg

func (ErrorUnprocessable) Error

func (e ErrorUnprocessable) Error() string

type ErrorValidation

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

func NewErrorValidationFf

func NewErrorValidationFf(msg string, msgArgs ...any) ErrorValidation

NewErrorValidationFf

Panics in case of empty arguments:

  • msg

func (ErrorValidation) Error

func (e ErrorValidation) Error() string

type SyncMap added in v0.4.0

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewSyncMap added in v0.4.0

func NewSyncMap[K comparable, V any]() *SyncMap[K, V]

func (*SyncMap[K, V]) Delete added in v0.4.0

func (m *SyncMap[K, V]) Delete(k K)

Delete

Removes association with the key `k`. Does nothing, if key `k` does not exist.

func (*SyncMap[K, V]) DeleteAndGet added in v0.4.0

func (m *SyncMap[K, V]) DeleteAndGet(k K) (V, bool)

DeleteAndGet

Removes association with the key `k` and then returns associated value like the Get() method does.

func (*SyncMap[K, V]) DeleteIf added in v0.6.0

func (m *SyncMap[K, V]) DeleteIf(k K, fn func(v V) bool)

DeleteIf

Removes association with the key `k`, if `fn` returns `true`. Does not call `fn`, if key `k` does not exist.

func (*SyncMap[K, V]) Get added in v0.4.0

func (m *SyncMap[K, V]) Get(k K) (V, bool)

func (*SyncMap[K, V]) Keys added in v0.4.0

func (m *SyncMap[K, V]) Keys() []K

func (*SyncMap[K, V]) Len added in v0.4.0

func (m *SyncMap[K, V]) Len() int

func (*SyncMap[K, V]) Modify added in v0.4.0

func (m *SyncMap[K, V]) Modify(k K, fn func(v V) V)

Modify

(Re-)Associates key `k` with value `v` modified by the `fn(v)`. If key `k` does not exist, `fn` gets zero-value argument.

func (*SyncMap[K, V]) Set added in v0.4.0

func (m *SyncMap[K, V]) Set(k K, v V)

Set

(Re-)Associates value `v` with the key `k`.

func (*SyncMap[K, V]) Truncate added in v0.4.0

func (m *SyncMap[K, V]) Truncate()

Truncate

Truncates the map.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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