utils

package
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2023 License: BSD-3-Clause Imports: 20 Imported by: 19

README

Utils

This package contains a good few number of helper functions which are commonly used in projects. We used some of them in RonyKIT internally and exposed them in this separate package to let other projects use it easily.

Some utilities this package provides:

  1. Random generators for String, Digits, ...
  2. SpinLock: a mutex-free lock
  3. Strings: transformation helper functions such as ToCamel, ToLowerCamel, ...
  4. LinkedList
  5. Hash: optimized version of Sha512 and Sha256 implementations of builtin hash functions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendStrInt

func AppendStrInt(sb *strings.Builder, x int)

func AppendStrInt32

func AppendStrInt32(sb *strings.Builder, x int32)

func AppendStrInt64

func AppendStrInt64(sb *strings.Builder, x int64)

func AppendStrUInt

func AppendStrUInt(sb *strings.Builder, x uint)

func AppendStrUInt32

func AppendStrUInt32(sb *strings.Builder, x uint32)

func AppendStrUInt64

func AppendStrUInt64(sb *strings.Builder, x uint64)

func B2S

func B2S(bts []byte) string

B2S is alias for ByteToStr.

func ByteToStr

func ByteToStr(bts []byte) string

ByteToStr converts byte slice to a string without memory allocation. Note it may break if string and/or slice header will change in the future go versions.

func CPUTicks

func CPUTicks() int64

CPUTicks is a faster alternative to NanoTime to measure time duration.

func CloneBytes added in v0.10.24

func CloneBytes(b []byte) []byte

func CloneStr added in v0.10.24

func CloneStr(s string) string

func CopyFile added in v0.10.19

func CopyFile(srcFile, dstFile string) error

func CopyFileWithBuffer added in v0.10.19

func CopyFileWithBuffer(srcFile, dstFile string, buf []byte) error

func FastRand

func FastRand() uint32

FastRand is a fast thread local random function.

func Float32ToStr added in v0.10.24

func Float32ToStr(x float32) string

func Float64ToStr added in v0.10.24

func Float64ToStr(x float64) string

func GetExecDir added in v0.10.19

func GetExecDir() string

func GetExecName added in v0.10.19

func GetExecName() string

func Int32ToStr

func Int32ToStr(x int32) string

func Int64ToStr

func Int64ToStr(x int64) string

func IntToStr

func IntToStr(x int) string

func MustSha256

func MustSha256(in, out []byte) []byte

func MustSha512

func MustSha512(in, out []byte) []byte

MustSha512 is Sha512 but it panics if any error happens

func NanoTime

func NanoTime() int64

NanoTime returns the current time in nanoseconds from a monotonic clock.

func RandomDigit

func RandomDigit(n int) string

RandomDigit generates a pseudo-random string with length 'n' which characters are only digits (0-9)

func RandomID

func RandomID(n int) string

RandomID generates a pseudo-random string with length 'n' which characters are alphanumerics.

func RandomIDs

func RandomIDs(n ...int) []string

func RandomInt

func RandomInt(n int) (x int)

func RandomInt32

func RandomInt32(n int32) (x int32)

RandomInt32 produces a pseudo-random 31bit number, if n == 0 there will be no limit otherwise the output will be smaller than n

func RandomInt64

func RandomInt64(n int64) (x int64)

RandomInt64 produces a pseudo-random 63bit number, if n == 0 there will be no limit otherwise the output will be smaller than n

func RandomUint64

func RandomUint64(n uint64) (x uint64)

RandomUint64 produces a pseudo-random unsigned number

func ReadYamlFile added in v0.10.19

func ReadYamlFile(filePath string, data any) error

func S2B

func S2B(str string) []byte

S2B is alias for StrToByte.

func SecureRandomInt63

func SecureRandomInt63(n int64) (x int64)

SecureRandomInt63 produces a secure pseudo-random 63bit number

func SecureRandomUint64

func SecureRandomUint64() (x uint64)

SecureRandomUint64 produces a secure pseudo-random 64bit number

func Sha256

func Sha256(in, out []byte) ([]byte, error)

Sha256 appends a 32bytes array which is sha256(in) to out

func Sha512

func Sha512(in, out []byte) ([]byte, error)

Sha512 appends a 64bytes array which is sha512(in) to out and returns out

func StrToByte

func StrToByte(str string) (b []byte)

StrToByte 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.

func StrToFloat32

func StrToFloat32(s string) float32

func StrToFloat64

func StrToFloat64(s string) float64

func StrToInt

func StrToInt(s string) int

func StrToInt32

func StrToInt32(s string) int32

func StrToInt64

func StrToInt64(s string) int64

func StrToUInt

func StrToUInt(s string) uint

func StrToUInt32

func StrToUInt32(s string) uint32

func StrToUInt64

func StrToUInt64(s string) uint64

func StrTruncate added in v0.9.26

func StrTruncate(s string, maxSize int) string

func TimeUnix

func TimeUnix() int64

func TimeUnixSubtract

func TimeUnixSubtract(unixTime int64, d time.Duration) int64

func ToCamel

func ToCamel(s string) string

ToCamel converts a string to CamelCase

func ToDelimited

func ToDelimited(s string, delimiter uint8) string

ToDelimited converts a string to delimited.snake.case (in this case `delimiter = '.'`)

func ToKebab

func ToKebab(s string) string

ToKebab converts a string to kebab-case

func ToLowerCamel

func ToLowerCamel(s string) string

ToLowerCamel converts a string to lowerCamelCase

func ToScreamingDelimited

func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string

ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE (in this case `delimiter = '.'; screaming = true`) or delimited.snake.case (in this case `delimiter = '.'; screaming = false`)

func ToScreamingKebab

func ToScreamingKebab(s string) string

ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE

func ToScreamingSnake

func ToScreamingSnake(s string) string

ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE

func ToSnake

func ToSnake(s string) string

ToSnake converts a string to snake_case

func ToSnakeWithIgnore

func ToSnakeWithIgnore(s string, ignore uint8) string

func UInt32ToStr

func UInt32ToStr(x uint32) string

func UInt64ToStr

func UInt64ToStr(x uint64) string

func WriteYamlFile added in v0.10.19

func WriteYamlFile(filePath string, data any) error

Types

type LinkedList

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

func NewLinkedList

func NewLinkedList() *LinkedList

func (*LinkedList) Append

func (ll *LinkedList) Append(data any)

func (*LinkedList) Get

func (ll *LinkedList) Get(index int32) (n *Node)

func (*LinkedList) Head

func (ll *LinkedList) Head() *Node

func (*LinkedList) PickHeadData

func (ll *LinkedList) PickHeadData() any

func (*LinkedList) PickTailData

func (ll *LinkedList) PickTailData() any

func (*LinkedList) Prepend

func (ll *LinkedList) Prepend(data any)

func (*LinkedList) RemoveAt

func (ll *LinkedList) RemoveAt(index int32)

func (*LinkedList) Reset

func (ll *LinkedList) Reset()

func (*LinkedList) Size

func (ll *LinkedList) Size() int32

func (*LinkedList) String

func (ll *LinkedList) String() string

func (*LinkedList) Tail

func (ll *LinkedList) Tail() *Node

type Node

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

func (Node) GetData

func (n Node) GetData() any

type Numeric added in v0.9.13

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

Numeric represents float64 number which is decodable from string, int or float. It's useful when a struct field should be numeric but form of the data being decoded from is unknown or variable.

func ParseNumeric added in v0.9.13

func ParseNumeric(src any) Numeric

ParseNumeric converts int, string, float to Numeric.

func (Numeric) MarshalJSON added in v0.10.27

func (n Numeric) MarshalJSON() ([]byte, error)

func (Numeric) String added in v0.9.13

func (n Numeric) String() string

func (*Numeric) UnmarshalJSON added in v0.9.13

func (n *Numeric) UnmarshalJSON(bb []byte) error

func (Numeric) Value added in v0.9.13

func (n Numeric) Value() float64

func (Numeric) WithPrecision added in v0.9.13

func (n Numeric) WithPrecision(p int) Numeric

func (Numeric) WithoutPrecision added in v0.9.13

func (n Numeric) WithoutPrecision() Numeric

type SpinLock

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

SpinLock is a spinlock implementation.

A SpinLock must not be copied after first use. This SpinLock intended to be used to synchronize exceptionally short-lived operations.

func (*SpinLock) Lock

func (l *SpinLock) Lock()

Lock locks l. If the lock is already in use, the calling goroutine blocks until the locker is available.

func (*SpinLock) Unlock

func (l *SpinLock) Unlock()

Unlock unlocks l.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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