utils

package
v0.0.0-...-048a247 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2023 License: Apache-2.0 Imports: 10 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BitField = bitField{}

Functions

func B2S

func B2S(b []byte) string

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

func Contains

func Contains[T comparable](arr []T, val T) bool

func DestructureMap

func DestructureMap[K comparable, V any](mp map[K]V) ([]K, []V)

func DifferentArray

func DifferentArray[T comparable](a []T, b []T) bool

func EmptyChannel

func EmptyChannel[T any](ch chan T)

func Filter

func Filter[T any](a []T, fn func(x T) bool) []T

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

GenerateRandomBytes returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateRandomString

func GenerateRandomString(s int) (string, error)

GenerateRandomString returns a URL-safe, base64 encoded securely generated random string. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func IsPointer

func IsPointer(v interface{}) bool

func IsPowerOfTwo

func IsPowerOfTwo(n int64) bool

Util - Is Power Of Two

func IsSliceArray

func IsSliceArray(v interface{}) bool

func IsSliceArrayPointer

func IsSliceArrayPointer(v interface{}) bool

func JitterTime

func JitterTime(lower, upper time.Duration) time.Duration

func Map

func Map[A any, T any](a []A, fn func(x A) T) []T

func PanicHandler

func PanicHandler(handle func(err interface{}))

func PointerOf

func PointerOf[T any](v T) *T

func PrependSlice

func PrependSlice[T any](s []T, v T) []T

func S2B

func S2B(s string) (b []byte)

S2B 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 SliceIndexOf

func SliceIndexOf[T comparable](arr []T, val T) int

func SliceRemove

func SliceRemove[T any](s []T, i int) []T

func Ternary

func Ternary[T any](condition bool, whenTrue T, whenFalse T) T

Util - Ternary: A golang equivalent to JS Ternary Operator

It takes a condition, and returns a result depending on the outcome

func ToAny

func ToAny(i any) any

func ToJSON

func ToJSON[T any](d T) []byte

ToJSON converts a struct to JSON, but instead of throwing an error will simply set the resulting JSON to an empty object if failed.

Types

type Color

type Color int32

Color is a utility to efficiently format colors into a 32-bit integer with alpha channel support

func (Color) GetAlpha

func (c Color) GetAlpha() uint8

GetAlpha returns the alpha channel of the color

func (Color) GetBlue

func (c Color) GetBlue() uint8

GetBlue returns the blue channel of the color

func (Color) GetGreen

func (c Color) GetGreen() uint8

SetGreen sets the green channel of the color

func (Color) GetRed

func (c Color) GetRed() uint8

GetRed returns the red channel of the color

func (Color) ParseHex

func (c Color) ParseHex(v string, alpha uint8) (Color, error)

ParseHex parses a hex string into a Color

func (Color) ParseRGB

func (c Color) ParseRGB(r, g, b, a uint8) Color

ParseHex parses red, green, blue into a Color

func (Color) SetAlpha

func (c Color) SetAlpha(v uint8) Color

SetAlpha sets the alpha channel of the color

func (Color) SetBlue

func (c Color) SetBlue(v uint8) Color

SetBlue sets the blue channel of the color

func (Color) SetGreen

func (c Color) SetGreen(v uint8) Color

SetGreen sets the green channel of the color

func (Color) SetRGB

func (c Color) SetRGB(r, g, b uint8) Color

SetRGB sets the red, green, and blue channels of the color

func (Color) SetRGBA

func (c Color) SetRGBA(r, g, b, a uint8) Color

SetRGBA sets the red, green, blue, and alpha channels of the color

func (Color) SetRed

func (c Color) SetRed(v uint8) Color

SetRed sets the red channel of the color

func (Color) Sum

func (c Color) Sum() int32

func (Color) ToHex

func (c Color) ToHex(alpha bool) string

ToHex returns the hex representation of the color

func (Color) ToRGB

func (c Color) ToRGB() [3]uint8

ToRGB returns the red, green, and blue channels of the color

func (Color) ToRGBA

func (c Color) ToRGBA() [4]uint8

ToRGBA returns the red, green, blue, and alpha channels of the color

type Key

type Key string

type Queue

type Queue[T any] interface {
	Add(val T)
	Pop() T
	Items() []T
	Clear()
	IsFull() bool
	IsEmpty() bool
}

func NewQueue

func NewQueue[T any](capacity int) Queue[T]

type Set

type Set[T comparable] map[T]struct{}

func (Set[T]) Add

func (s Set[T]) Add(val T)

func (Set[T]) Delete

func (s Set[T]) Delete(val T)

func (Set[T]) Fill

func (s Set[T]) Fill(vals ...T)

func (Set[T]) Has

func (s Set[T]) Has(val T) bool

func (Set[T]) Values

func (s Set[T]) Values() []T

type Throttler

type Throttler interface {
	Do(f func())
}

func NewThrottle

func NewThrottle(duration time.Duration) Throttler

Jump to

Keyboard shortcuts

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