Documentation
¶
Index ¶
- Constants
- Variables
- func AppendableFile(name string) (*os.File, error)
- func CamelCase(s string) string
- func CliRun(ex string, args ...string) ([]byte, error)
- func CollectFields(ptr any) []reflect.StructField
- func CollectTypeFields(eleType reflect.Type) []reflect.StructField
- func CopyFilter[T any](l []T, f func(T) bool) []T
- func DecodeJson(reader io.Reader, ptr any) error
- func Distinct(l []string) []string
- func ERand(len int) string
- func EncodeJson(writer io.Writer, body any) error
- func FastDistinct(l []string) []string
- func FileExists(path string) (bool, error)
- func Filter[T any](l []T, f func(T) bool) []T
- func FuncName(f any) string
- func FuzzParseTime(formats []string, value string) (time.Time, error)
- func GenId() (id string)
- func GenIdP(prefix string) (id string)
- func GenNo(prefix string) string
- func GenNoL(prefix string, len int) string
- func HasPrefixIgnoreCase(s string, prefix string) bool
- func HasSuffixIgnoreCase(s string, suffix string) bool
- func IsBlankStr(s string) bool
- func IsBool(boolStr string) bool
- func IsFieldExposed(fieldName string) bool
- func IsTrue(boolStr string) bool
- func IsVoid(t reflect.Type) bool
- func LastNStr(s string, n int) string
- func LowercaseNamingStrategy(name string) string
- func MapFirst[K comparable, V any](m map[K]V) V
- func MapKeys[T comparable, V any](m map[T]V) []T
- func MapValues[K comparable, V any](m map[K]V) []V
- func MaxInt(a int, b int) int
- func MaxLenStr(s string, max int) string
- func MergeStrPairs(p ...StrPair) map[string][]any
- func MinInt(a int, b int) int
- func MkdirAll(path string) error
- func MkdirParentAll(path string) error
- func NewSubmitAsyncFunc[T any](pool *AsyncPool) func(task func() (T, error)) Future[T]
- func NewVar[T any]() T
- func OpenFile(name string, flag int) (*os.File, error)
- func PadNum(n int, digit int) string
- func ParseJson(body []byte, ptr any) error
- func Pick(set []rune) rune
- func Printlnf(pat string, args ...any)
- func RandAlpha(n int) string
- func RandLowerAlpha(n int) string
- func RandLowerAlphaNumeric(n int) string
- func RandLowerAlphaNumeric16() string
- func RandNum(n int) string
- func RandStr(n int) string
- func RandUpperAlpha(n int) string
- func RandUpperAlphaNumeric(n int) string
- func ReadFileAll(path string) ([]byte, error)
- func ReadWriteFile(name string) (*os.File, error)
- func SWriteJson(body any) (string, error)
- func SetMachineCode(code int) error
- func SetToSlice[T comparable](s Set[T]) []T
- func ShuffleRunes(letters []rune, times int) []rune
- func ShuffleStr(letters string, times int) string
- func SliceFilterFirst[T any](items []T, f func(T) bool) (T, bool)
- func SliceGetOne[T any](items []*T) *T
- func SliceMap[T any, V any](ts []T, mapFunc func(t T) V) []V
- func Spaces(count int) string
- func StrMap[T any, V any](l []T, keyMapper func(T) string, valueMapper func(T) V) map[string]V
- func Tabs(count int) string
- func TermOpenUrl(url string) error
- func ToStr(v any) string
- func TypeName(t reflect.Type) string
- func UnsafeByt2Str(b []byte) string
- func UnsafeStr2Byt(s string) (b []byte)
- func WalkTagShallow(ptr any, callbacks ...WalkTagCallback) error
- func WriteJson(body any) ([]byte, error)
- type AsyncPool
- type AwaitFutures
- type ETime
- func (t ETime) FormatClassic() string
- func (t ETime) FormatClassicLocale() string
- func (t ETime) MarshalJSON() ([]byte, error)
- func (et *ETime) Scan(value interface{}) error
- func (t ETime) String() string
- func (t ETime) ToTime() time.Time
- func (t ETime) UnixMilli() int64
- func (t *ETime) UnmarshalJSON(b []byte) error
- func (et ETime) Value() (driver.Value, error)
- type ForEachField
- type Future
- type IndWritef
- type IndentWriter
- type Introspector
- func (it *Introspector) Field(fieldName string) (field reflect.StructField, isFieldFound bool)
- func (it *Introspector) FieldAt(idx int) (field reflect.StructField)
- func (it *Introspector) FieldIdx(fieldName string) (index int, isFieldFound bool)
- func (it *Introspector) IterFields(forEach ForEachField)
- func (it *Introspector) Tag(fieldName string, tagName string) (tag string, isFieldFound bool)
- func (it *Introspector) TagRetriever(fieldName string) (t TagRetriever, isFieldFound bool)
- type Opt
- type Pair
- type RWMap
- type SLPinter
- type Set
- type StrPair
- type StrSliceFlag
- type TagRetriever
- type Void
- type WalkTagCallback
Constants ¶
const ( //lint:ignore U1000 ansi color code, keep it for future use ANSIRed = "\033[1;31m" ANSIGreen = "\033[1;32m" ANSICyan = "\033[1;36m" ANSIReset = "\033[0m" )
https://stackoverflow.com/questions/8357203/is-it-possible-to-display-text-in-a-console-with-a-strike-through-effect https://en.wikipedia.org/wiki/ANSI_escape_code
const ( BoolStrTrue = "true" BoolStrFalse = "false" )
const (
DEFAULT_LEN = 35
)
const (
// Default File Mode
DefFileMode = 0666
)
Variables ¶
var (
ErrGetTimeout = errors.New("future.TimedGet timeout")
)
Functions ¶
func AppendableFile ¶
Create appendable file with 0666 permission.
func CollectTypeFields ¶
func CollectTypeFields(eleType reflect.Type) []reflect.StructField
Get Fields of A Type
func CopyFilter ¶
Filter slice value, the original slice is copied before filtering.
func FastDistinct ¶
Filter duplicate values, faster but values are sorted, and the slice values are filtered in place.
func Filter ¶
Filter slice values in place. Be cautious that both slices are backed by the same array.
func GenId ¶
func GenId() (id string)
Generate Id
The id consists of [64 bits long] + [6 digits machine_code] The 64 bits long consists of: [sign bit (1 bit)] + [timestamp (49 bits, ~1487.583 years)] + [sequenceNo (14 bits, 0~16383)]
The max value of Long is 9223372036854775807, which is a string with 19 characters, so the generated id will be of at most 25 characters ¶
This func is thread-safe
func GenIdP ¶
Generate Id with prefix
The id consists of [64 bits long] + [6 digits machine_code] The 64 bits long consists of: [sign bit (1 bit)] + [timestamp (49 bits, ~1487.583 years)] + [sequenceNo (14 bits, 0~16383)]
The max value of Long is 9223372036854775807, which is a string with 19 characters, so the generated id will be of at most 25 characters ¶
This func is thread-safe
func HasPrefixIgnoreCase ¶
Check if s has the prefix in a case-insensitive way.
func HasSuffixIgnoreCase ¶
Check if s has the suffix in a case-insensitive way.
func LowercaseNamingStrategy ¶
Change first rune to lower case.
func MkdirParentAll ¶
MkdirAll but only for the parent directory of the path, perm 0755 is used.
The path should always point to a specific file under some directories, as this method always attempts to extract parent dir of the file. It the path fails to fulfill this requirement, the output might be unexpected.
func NewSubmitAsyncFunc ¶
Create func that calls SubmitAsync(...) with the given pool.
func RandAlpha ¶
Generate random alphabetic string with specified length
the generated string will contains [a-zA-Z]
func RandLowerAlpha ¶
Generate random alphabetic, lowercase string with specified length
the generated string will contains [a-z]
func RandLowerAlphaNumeric ¶
Generate random alphabetic, lowercase string with specified length
the generated string will contains [a-z0-9]
func RandLowerAlphaNumeric16 ¶
func RandLowerAlphaNumeric16() string
Same as RandLowerAlphaNumeric(16) but with less allocation.
func RandNum ¶
Generate random numeric string with specified length
the generated string will contains [0-9]
func RandStr ¶
Generate random string with specified length
the generated string will contains [a-zA-Z0-9]
ERand() is preferred for higher entrophy
func RandUpperAlpha ¶
Generate random alphabetic, uppercase string with specified length
the generated string will contains [A-Z]
func RandUpperAlphaNumeric ¶
Generate random alphabetic, uppercase string with specified length
the generated string will contains [A-Z0-9]
func ReadWriteFile ¶
Create readable & writable file with 0666 permission.
func SetMachineCode ¶
Overwrite the randomly generated machine code, machine code must be between 0 and 999999, at most 6 digits.
func ShuffleRunes ¶
func ShuffleStr ¶
func SliceFilterFirst ¶
Select one from the slice that matches the condition.
func TermOpenUrl ¶
func UnsafeByt2Str ¶
Convert []byte to string without alloc.
Both the []byte and the string share the same memory.
Any modification on the original []byte is reflected on the returned string.
byt = []byte("abc")
s = UnsafeByt2Str(byt) // "abc" using the same memory
byt[0] = 'd' // modified in place at 0, also reflected on s ("dbc")
Tricks from https://github.com/valyala/fasthttp.
func UnsafeStr2Byt ¶
Convert string to []byte without alloc.
Both the []byte and the string share the same memory.
The resulting []byte is not modifiable, program will panic if modified.
s := "abc" byt := UnsafeStr2Byt(s) // "abc" but in []byte byt[0] = 'd' // will panic
Tricks from https://github.com/valyala/fasthttp.
func WalkTagShallow ¶
func WalkTagShallow(ptr any, callbacks ...WalkTagCallback) error
Walk fields of *struct, won't go deeper even if the field is a struct.
Types ¶
type AsyncPool ¶
type AsyncPool struct {
// contains filtered or unexported fields
}
A long live, bounded pool of goroutines.
Use miso.NewAsyncPool to create a new pool.
AsyncPool internally maintains a task queue with limited size and limited number of workers. If the task queue is full, the caller of *AsyncPool.Go is blocked indefinitively.
func NewAsyncPool ¶
Create a bounded pool of goroutines.
The maxTasks determines the capacity of the task queues. If the task queue is full, the caller of *AsyncPool.Go is blocked.
The maxWorkers determines the max number of workers.
type AwaitFutures ¶
type AwaitFutures[T any] struct { // contains filtered or unexported fields }
AwaitFutures represent multiple tasks that will be submitted to the pool asynchronously whose results will be awaited together.
AwaitFutures should only be used once everytime it's needed.
Use miso.NewAwaitFutures() to create one.
func NewAwaitFutures ¶
func NewAwaitFutures[T any](pool *AsyncPool) *AwaitFutures[T]
func (*AwaitFutures[T]) Await ¶
func (a *AwaitFutures[T]) Await() []Future[T]
func (*AwaitFutures[T]) SubmitAsync ¶
func (a *AwaitFutures[T]) SubmitAsync(task func() (T, error))
type ETime ¶
EpochTime, same as time.Time but will be serialized/deserialized as epoch milliseconds
This type can be safely used in GORM just like time.Time
func (ETime) FormatClassic ¶
func (ETime) FormatClassicLocale ¶
func (ETime) MarshalJSON ¶
func (*ETime) UnmarshalJSON ¶
implements decorder.Unmarshaler in encoding/json.
type ForEachField ¶
type ForEachField func(index int, field reflect.StructField) (breakIteration bool)
type Future ¶
type IndentWriter ¶
func NewIndentWriter ¶
func NewIndentWriter(indentStr string) IndentWriter
func (*IndentWriter) StepIn ¶
func (i *IndentWriter) StepIn(f func(iw *IndentWriter)) *IndentWriter
func (*IndentWriter) Writef ¶
func (i *IndentWriter) Writef(pat string, args ...any) *IndentWriter
type Introspector ¶
type Introspector struct {
Type reflect.Type
Fields []reflect.StructField
// contains filtered or unexported fields
}
func (*Introspector) Field ¶
func (it *Introspector) Field(fieldName string) (field reflect.StructField, isFieldFound bool)
Get field by name
func (*Introspector) FieldAt ¶
func (it *Introspector) FieldAt(idx int) (field reflect.StructField)
Get field at index
func (*Introspector) FieldIdx ¶
func (it *Introspector) FieldIdx(fieldName string) (index int, isFieldFound bool)
Get field index
func (*Introspector) IterFields ¶
func (it *Introspector) IterFields(forEach ForEachField)
Iterate fields
func (*Introspector) Tag ¶
func (it *Introspector) Tag(fieldName string, tagName string) (tag string, isFieldFound bool)
Get tag by of field
func (*Introspector) TagRetriever ¶
func (it *Introspector) TagRetriever(fieldName string) (t TagRetriever, isFieldFound bool)
Get tag retriever for a field
type RWMap ¶
type RWMap[K comparable, V any] struct { // contains filtered or unexported fields }
Map with sync.RWMutex embeded.
type Set ¶
type Set[T comparable] struct { // Keys in Set Keys map[T]Void }
Set data structure
It's internally backed by a Map.
To create a new Set, use NewSet() func.
type StrSliceFlag ¶
type StrSliceFlag []string
func FlagStrSlice ¶
func FlagStrSlice(name string, usage string) *StrSliceFlag
func (*StrSliceFlag) Set ¶
func (s *StrSliceFlag) Set(t string) error
func (*StrSliceFlag) String ¶
func (s *StrSliceFlag) String() string