Documentation
¶
Index ¶
- func AddUnique[T comparable](s []T, v T) []T
- func AppendStrInt(sb *strings.Builder, x int)
- func AppendStrInt32(sb *strings.Builder, x int32)
- func AppendStrInt64(sb *strings.Builder, x int64)
- func AppendStrUInt(sb *strings.Builder, x uint)
- func AppendStrUInt32(sb *strings.Builder, x uint32)
- func AppendStrUInt64(sb *strings.Builder, x uint64)
- func ArrayToMap[V any](s []V) map[int]V
- func ArrayToMapFunc[K comparable, V any](s []V, fn func(V) K) map[K]V
- func ArrayToSet[T comparable](s []T) map[T]struct{}
- func B2S(bts []byte) string
- func ByteToStr(bts []byte) string
- func CPUTicks() int64
- func Cast[T any](val any) T
- func CastJSON[T any](val any) T
- func CloneBytes(b []byte) []byte
- func CloneStr(s string) string
- func Coalesce[T comparable](in ...T) T
- func Contains[T comparable](s []T, v T) bool
- func ContainsAll[T comparable](s []T, v []T) bool
- func ContainsAny[T comparable](s []T, v []T) bool
- func CopyFile(srcFile, dstFile string) error
- func CopyFileWithBuffer(srcFile, dstFile string, buf []byte) error
- func DynCast[DST, SRC any](src SRC, mappingPair ...string) DST
- func DynCastOption[DST, SRC any](src SRC, converters []TypeConverter, mappingPair ...string) DST
- func F32ToStr(x float32) string
- func F64ToStr(x float64) string
- func FastRand() uint32
- func Filter[T any](tt []T, match func(src T) bool) []T
- func First[K, V comparable](in map[K]V, keys ...K) (V, bool)
- func FirstOr[K, V comparable](def V, in map[K]V, keys ...K) V
- func Float32ToStr(x float32) string
- func Float64ToStr(x float64) string
- func ForEach[V any](in []V, fn func(*V))
- func FromJSON[T any](bytes []byte) T
- func GetExecDir() string
- func GetExecName() string
- func Int32ToStr(x int32) string
- func Int64ToStr(x int64) string
- func IntToStr(x int) string
- func Map[S, D any](ss []S, transformer func(src S) D) []D
- func MapKeysToArray[K comparable, V any](s map[K]V) []K
- func MapToArray[K comparable, V any](s map[K]V) []V
- func MapToArrayFunc[K comparable, V any, T any](s map[K]V, fn func(K, V) T) []T
- func Must[T any](src T, err error) T
- func MustSha256(in, out []byte) []byte
- func MustSha512(in, out []byte) []byte
- func NanoTime() int64
- func Ok[T any](v T, _ error) T
- func OkOr[T any](v T, err error, fallback T) T
- func Paginate[T any](arr []T, pageSize int, fn func(start, end int) error) error
- func PtrVal[T any](src *T) T
- func RandomDigit(n int) string
- func RandomID(n int) string
- func RandomIDs(n ...int) []string
- func RandomInt(n int) (x int)
- func RandomInt32(n int32) (x int32)
- func RandomInt64(n int64) (x int64)
- func RandomUint64(n uint64) (x uint64)
- func ReadYamlFile(filePath string, data any) error
- func Reduce[T, R any](reducer func(r R, t T) R, tt []T) R
- func S2B(str string) []byte
- func SecureRandomInt63(n int64) (x int64)
- func SecureRandomUint64() (x uint64)
- func Sha256(in, out []byte) ([]byte, error)
- func Sha512(in, out []byte) ([]byte, error)
- func StrToByte(str string) (b []byte)
- func StrToFloat32(s string) float32
- func StrToFloat64(s string) float64
- func StrToInt(s string) int
- func StrToInt32(s string) int32
- func StrToInt64(s string) int64
- func StrToUInt(s string) uint
- func StrToUInt32(s string) uint32
- func StrToUInt64(s string) uint64
- func StrTruncate(s string, maxSize int) string
- func TimeUnix() int64
- func TimeUnixAdd(unixTime int64, d time.Duration) int64
- func TimeUnixSubtract(unixTime int64, d time.Duration) int64
- func ToCamel(s string) string
- func ToDelimited(s string, delimiter uint8) string
- func ToJSON(val any) []byte
- func ToKebab(s string) string
- func ToLowerCamel(s string) string
- func ToMap(s any) map[string]any
- func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string
- func ToScreamingKebab(s string) string
- func ToScreamingSnake(s string) string
- func ToSnake(s string) string
- func ToSnakeWithIgnore(s string, ignore uint8) string
- func TryCast[T any](v any) T
- func UInt32ToStr(x uint32) string
- func UInt64ToStr(x uint64) string
- func UIntToStr(x uint) string
- func ValPtr[T any](src T) *T
- func ValPtrOrNil[T comparable](src T) *T
- func VisitAll[VisitorState any](initial VisitorState, visitors ...func(ctx *VisitorState)) VisitorState
- func VisitCond[VisitorState any](initial VisitorState, cond func(ctx *VisitorState) bool, ...) VisitorState
- func VisitStopOnErr[VisitorState any](initial VisitorState, visitors ...func(ctx *VisitorState) error) (VisitorState, error)
- func WriteYamlFile(filePath string, data any) error
- type LinkedList
- func (ll *LinkedList) Append(data any)
- func (ll *LinkedList) Get(index int32) (n *Node)
- func (ll *LinkedList) Head() *Node
- func (ll *LinkedList) PickHeadData() any
- func (ll *LinkedList) PickTailData() any
- func (ll *LinkedList) Prepend(data any)
- func (ll *LinkedList) RemoveAt(index int32)
- func (ll *LinkedList) Reset()
- func (ll *LinkedList) Size() int32
- func (ll *LinkedList) String() string
- func (ll *LinkedList) Tail() *Node
- type Node
- type Numeric
- type Result
- type SingleFlightCall
- type SpinLock
- type TypeConverter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddUnique ¶
func AddUnique[T comparable](s []T, v T) []T
func AppendStrInt ¶
func AppendStrInt32 ¶
func AppendStrInt64 ¶
func AppendStrUInt ¶
func AppendStrUInt32 ¶
func AppendStrUInt64 ¶
func ArrayToMap ¶
ArrayToMap converts a slice to a map with the index as the key.
func ArrayToMapFunc ¶ added in v0.20.19
func ArrayToMapFunc[K comparable, V any](s []V, fn func(V) K) map[K]V
func ArrayToSet ¶
func ArrayToSet[T comparable](s []T) map[T]struct{}
ArrayToSet converts a slice to a set (i.e. map).
func ByteToStr ¶
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 Cast ¶
Cast Converts any type to a given type. If conversion fails, it returns the zero value of the given type.
func CastJSON ¶
CastJSON Converts any type to a given type based on their json representation. It partially fills the target in case they are not directly compatible.
func CloneBytes ¶
func Coalesce ¶
func Coalesce[T comparable](in ...T) T
Coalesce returns the first non-zero value from the input list.
func Contains ¶
func Contains[T comparable](s []T, v T) bool
func ContainsAll ¶
func ContainsAll[T comparable](s []T, v []T) bool
func ContainsAny ¶
func ContainsAny[T comparable](s []T, v []T) bool
func CopyFileWithBuffer ¶
func DynCastOption ¶
func DynCastOption[DST, SRC any]( src SRC, converters []TypeConverter, mappingPair ...string, ) DST
func Filter ¶
Filter returns a new slice containing only the elements in tt for which the match function returns true.
type User struct { Name string Age int Active bool } var users = []User{ {"Tom", 20, true}, {"Jack", 22, false}, {"Mary", 18, true}, } var activeUsers = qkit.Filter(users, func(u User) bool { return u.Active })
fmt.Println(activeUsers)
Playground: https://go.dev/play/p/70YOKRs79OF
func First ¶
func First[K, V comparable](in map[K]V, keys ...K) (V, bool)
First returns the first value found in the map for the given keys.
func FirstOr ¶
func FirstOr[K, V comparable](def V, in map[K]V, keys ...K) V
func Float32ToStr ¶
func Float64ToStr ¶
func GetExecDir ¶
func GetExecDir() string
func GetExecName ¶
func GetExecName() string
func Int32ToStr ¶
func Int64ToStr ¶
func Map ¶
func Map[S, D any](ss []S, transformer func(src S) D) []D
Map applies the transformer function to each element of the slice ss. The result is a slice of the same length as ss, where the kth element is transformer(ss[k]).
type User struct { Name string Age int } var users = []User{ {"Tom", 20}, {"Jack", 22}, {"Mary", 18}, } var names = qkit.Map(users, func(u User) string { return u.Name })
fmt.Println(names)
Playground: https://go.dev/play/p/wKIa32-rMDn
func MapKeysToArray ¶
func MapKeysToArray[K comparable, V any](s map[K]V) []K
MapKeysToArray converts a map's keys to a slice.
func MapToArray ¶
func MapToArray[K comparable, V any](s map[K]V) []V
MapToArray converts a map's values to a slice.
func MapToArrayFunc ¶
func MapToArrayFunc[K comparable, V any, T any](s map[K]V, fn func(K, V) T) []T
MapToArrayFunc converts a map's values to a slice using the given function.
func MustSha256 ¶
func MustSha512 ¶
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 Paginate ¶
Paginate will call the given function with start and end indexes for a slice of the given size.
type User struct { Name string Age int } var users = []User{ {"Tom", 20}, {"Jack", 22}, {"Mary", 18}, {"Tommy", 20}, {"Lin", 22}, } qkit.Paginate(users, 2, func(start, end int) error { fmt.Println(users[start:end]) return nil })
Playground: https://go.dev/play/p/aDiVJEKjgwW
func PtrVal ¶
func PtrVal[T any](src *T) T
PtrVal returns the value of the pointer src. It is a dereference operation.
func RandomDigit ¶
RandomDigit generates a pseudo-random string with length 'n' which characters are only digits (0-9).
func RandomID ¶
RandomID generates a pseudo-random string with length 'n' which characters are alphanumerics.
func RandomInt32 ¶
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 ¶
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 ¶
RandomUint64 produces a pseudo-random unsigned number.
func ReadYamlFile ¶
func Reduce ¶
func Reduce[T, R any](reducer func(r R, t T) R, tt []T) R
Reduce [T, R] reduces the slice tt to a single value r using the reducer function. The reducer function takes the current reduced value r and the current slice value t and returns a new reduced value.
type User struct { Name string Age int } var users = []User{ {"Tom", 20}, {"Jack", 22}, {"Mary", 18}, } var totalAge = qkit.Reduce(func(r int, u User) int { return r + u.Age }, users)
fmt.Println(totalAge)
Playground: https://go.dev/play/p/gf9evzMIMIK
func SecureRandomInt63 ¶
SecureRandomInt63 produces a secure pseudo-random 63bit number.
func SecureRandomUint64 ¶
func SecureRandomUint64() (x uint64)
SecureRandomUint64 produces a secure pseudo-random 64bit number.
func StrToByte ¶
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 StrToFloat64 ¶
func StrToInt32 ¶
func StrToInt64 ¶
func StrToUInt32 ¶
func StrToUInt64 ¶
func StrTruncate ¶
func ToDelimited ¶
ToDelimited converts a string to delimited.snake.case (in this case `delimiter = '.'`).
func ToLowerCamel ¶
ToLowerCamel converts a string to lowerCamelCase.
func ToScreamingDelimited ¶
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 ¶
ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE.
func ToScreamingSnake ¶
ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE.
func ToSnakeWithIgnore ¶
func TryCast ¶
TryCast tries to cast the input value to the target type. If the cast fails, it returns the zero value of the target type.
func UInt32ToStr ¶
func UInt64ToStr ¶
func ValPtr ¶
func ValPtr[T any](src T) *T
ValPtr returns the pointer of the src. It is a reference operation.
func ValPtrOrNil ¶
func ValPtrOrNil[T comparable](src T) *T
ValPtrOrNil returns the pointer of the src if src is not zero value, otherwise nil.
func VisitAll ¶
func VisitAll[VisitorState any]( initial VisitorState, visitors ...func(ctx *VisitorState), ) VisitorState
VisitAll runs all visitors and returns the final state.
func VisitCond ¶
func VisitCond[VisitorState any]( initial VisitorState, cond func(ctx *VisitorState) bool, visitors ...func(ctx *VisitorState), ) VisitorState
VisitCond runs all visitors if the condition is true and returns the final state If the condition is false, the visitor will stop and DO NOT run the rest of the visitors. cond function is called before each visitor.
NOTE: `cond` is called before each visitor, hence, it will be run on initial state too.
func VisitStopOnErr ¶
func VisitStopOnErr[VisitorState any]( initial VisitorState, visitors ...func(ctx *VisitorState) error, ) (VisitorState, error)
VisitStopOnErr runs all visitors and returns the final state If any of the visitors returns an error, the visitor will stop and DO NOT run the rest of the visitors. It returns the latest state and the error.
func WriteYamlFile ¶
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 Numeric ¶
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 ¶
ParseNumeric converts int, string, float to Numeric.
func (*Numeric) MarshalJSON ¶
func (*Numeric) UnmarshalJSON ¶
func (*Numeric) WithPrecision ¶
func (*Numeric) WithoutPrecision ¶
type SingleFlightCall ¶
func SingleFlight ¶
func SingleFlight[T any]() SingleFlightCall[T]
SingleFlight executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results. The return value shared indicates whether v was given to multiple cal.
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.
type TypeConverter ¶
type TypeConverter = copier.TypeConverter
func TypeConvert ¶
func TypeConvert[SRC, DST any](fn func(src SRC) (DST, error)) TypeConverter