utils

package
v0.0.0-...-1af4f67 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(x ...interface{})

Assert is an assert function, that is absent in GoLang because of https://golang.org/doc/faq#assertions

func Check

func Check(err error)

Check prints a given error to log, if it exists (othersise method does nothing)

func CheckPassword

func CheckPassword(hash, password, salt string) bool

CheckPassword verifies that a given "hash" is produced by a given "password"/"salt" pair with BCrypt/SCrypt algorithm

func Convert

func Convert(t time.Time) time.Time

Convert converts local time "t" to UTC+0 time

func GetErrorCode

func GetErrorCode(err *Error) byte

GetErrorCode returns a error code of a given Error

func GetHash

func GetHash(password, salt string) string

GetHash calculates crypto hash function for a given "password"/"salt" pair with BCrypt/SCrypt algorithm

func Max

func Max(a, b uint) uint

Max returns a maximum among integer numbers "a" and "b"

func MaxF

func MaxF(a, b float32) float32

MaxF returns a maximum among float numbers "a" and "b"

func Min

func Min(a, b uint) uint

Min returns a minimum among integer numbers "a" and "b"

func MinF

func MinF(a, b float32) float32

MinF returns a minimum among float numbers "a" and "b"

func RandString

func RandString(n int) string

RandString generates a new Random string of length "n" containing latin characters and digits. Taken from https://stackoverflow.com/questions/22892120

func RunDaemon

func RunDaemon(name string, t time.Duration, f func()) chan bool

RunDaemon runs a "background service" that calls function "f" periodically with intervals of "t". Method returns a channel[Bool], so that the daemon can be stopped with "stopMyDaemon <- true" "name" - name of a daemon [optional]

func RunTask

func RunTask(name string, t time.Duration, f func()) chan bool

RunTask starts a "background timer" that will call function "f" after a given duration "t". Method returns a channel[Bool], so that the timer can be stopped in advance with "stopMyTimer <- true" "name" - name of a timer [optional]

func Ternary

func Ternary(condition bool, x, y byte) byte

Ternary is a ternary operator for byte operands. Recall that ternary operator is absent in GoLang because of https://golang.org/doc/faq#Does_Go_have_a_ternary_form. Please be careful, because all the arguments are evaluated! E.g. "b := Ternary(obj != nil, obj.value, 0)" will produce NullPointerException, if obj equals to NULL

func TernaryInt

func TernaryInt(condition bool, x, y int) int

TernaryInt is a ternary operator for integer operands. Recall that ternary operator is absent in GoLang because of https://golang.org/doc/faq#Does_Go_have_a_ternary_form. Please be careful, because all the arguments are evaluated! E.g. "b := Ternary(obj != nil, obj.value, 0)" will produce NullPointerException, if obj equals to NULL

Types

type Error

type Error struct {
	Code   byte
	Text   string
	Origin interface{}
}

Error is a extension of standard Go "error"

func NewErr

func NewErr(who interface{}, code byte, txt string, args ...interface{}) *Error

NewErr creates a new Error instance from the scratch "who" - the owner component "code" - error code "txt" - error text with optional arguments like %d, %s, etc. "args" - arguments [optional]

func NewErrFromError

func NewErrFromError(who interface{}, code byte, err error) *Error

NewErrFromError creates a new Error instance from the existing Golang error. "who" - the owner component "code" - error code "err" - base error

func NewErrs

func NewErrs(errs ...*Error) *Error

NewErrs creates a new compound Error from several another Errors. The first instance is considered to be main, and the other ones will be appended by semicolon with corresponding codes and descriptions. "errs" - errors

func (*Error) Error

func (e *Error) Error() string

Error returns an error description as a string

type TryMutex

type TryMutex struct {
	sync.Mutex
	// contains filtered or unexported fields
}

TryMutex is an extension of Golang sync.Mutex

func (*TryMutex) OnlyOne

func (x *TryMutex) OnlyOne(f func())

OnlyOne ensures that a given function will be executed only once, even though multiple threads are trying to call it in the same time. If race condition happens, one thread will call "f", and the others will skip execution.

Jump to

Keyboard shortcuts

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