Documentation ¶
Index ¶
- func Caller(skip int) (name, file string, line int)
- func CallerName() string
- func ConvInts[T1, T2 constraints.Integer](slice []T1) []T2
- func Diff[S ~[]E, E comparable](slice S, others ...S) S
- func DiffInplace[S ~[]E, E comparable](slice S, others ...S) S
- func DiffInt32s(a []int32, b []int32) []int32deprecated
- func DiffInt64s(a []int64, b []int64) []int64deprecated
- func DiffMaps[M ~map[K]V, K comparable, V any](m M, others ...M) M
- func DiffMapsInplace[M ~map[K]V, K comparable, V any](m M, others ...M) M
- func DiffStrings(a []string, b []string) []stringdeprecated
- func DoRequest(req *Request) (header http.Header, respContent []byte, status int, err error)deprecated
- func EnsureError(v interface{}) error
- func Filter[S ~[]E, E any](predicate func(i int, elem E) bool, slices ...S) S
- func FilterInt32s(slice []int32, predicate func(i int) bool) []int32deprecated
- func FilterInt64s(slice []int64, predicate func(i int) bool) []int64deprecated
- func FilterMaps[M ~map[K]V, K comparable, V any](predicate func(k K, v V) bool, maps ...M) M
- func FilterStrings(slice []string, predicate func(i int) bool) []stringdeprecated
- func FormatInts[T constraints.Integer](slice []T, base int) []string
- func GetOutboundIP() (net.IP, error)
- func IdentifyPanic() string
- func InInt32s(slice []int32, elem int32) bool
- func InInt64s(slice []int64, elem int64) bool
- func InStrings(slice []string, elem string) bool
- func IndexFunc[E any](slice []E, predicate func(i int) bool) int
- func IntKeys[M ~map[K]V, K constraints.Integer, V any](m M) (keys []int64)deprecated
- func IntValues[M ~map[K]V, K comparable, V constraints.Integer](m M) (values []int64)deprecated
- func JSON(v interface{}) string
- func Keys[M ~map[K]V, K comparable, V any](m M, filter ...func(K, V) bool) []K
- func LastIndexFunc[E any](slice []E, predicate func(i int) bool) int
- func MapKeys[M ~map[K]V, K comparable, V any](m M) interface{}deprecated
- func MapValues[M ~map[K]V, K comparable, V any](m M) interface{}deprecated
- func MatchGroups(re *regexp.Regexp, str []byte) map[string][]byte
- func MatchStringGroups(re *regexp.Regexp, str string) map[string]string
- func MergeMaps[M ~map[K]V, K comparable, V any](maps ...M) M
- func MergeMapsTo[M ~map[K]V, K comparable, V any](dst M, others ...M) M
- func PanicOnError(args ...interface{})
- func ParseHTMLTemplates(rootDir string, rePattern string, funcMap htmltemplate.FuncMap) (*htmltemplate.Template, error)
- func ParseInts[T constraints.Integer](slice []string, base int) []T
- func ParseTextTemplates(rootDir string, rePattern string, funcMap texttemplate.FuncMap) (*texttemplate.Template, error)
- func Pretty(v interface{}) string
- func Pretty2(v interface{}) string
- func Recover(f func(ctx context.Context, panicErr *PanicError)) func(ctx context.Context)
- func Reverse[S ~[]E, E any](s S, inplace bool) S
- func ReverseInt32s(slice []int32, inplace bool) []int32deprecated
- func ReverseInt64s(slice []int64, inplace bool) []int64deprecated
- func ReverseStrings(slice []string, inplace bool) []stringdeprecated
- func Safe(f func()) func() error
- func Safe1(f func() error) func() error
- func SetDefault(dst interface{}, value ...interface{})
- func SingleJoin(sep string, text ...string) string
- func SlashJoin(path ...string) string
- func Sort[S ~[]E, E constraints.Ordered](s S) S
- func SortDesc[S ~[]E, E constraints.Ordered](s S) S
- func Split[S ~[]E, E any](slice S, batch int) []S
- func SplitInt32s(slice []int32, batch int) [][]int32
- func SplitInt64s(slice []int64, batch int) [][]int64
- func SplitSlice[S ~[]E, E any](slice S, batch int) interface{}deprecated
- func SplitStrings(slice []string, batch int) [][]string
- func StringKeys[M ~map[K]V, K ~string, V any](m M) (keys []string)deprecated
- func StringValues[M ~map[K]V, K comparable, V ~string](m M) (values []string)deprecated
- func Sum[T constraints.Integer](slice []T) int64
- func ToHashSet[S ~[]E, E comparable](slice S) map[E]bool
- func ToInterfaceSlice[S ~[]E, E any](slice S) []interface{}
- func ToMap[S ~[]E, E any, K comparable, V any](slice S, f func(E) (K, V)) map[K]V
- func Unique[S ~[]E, E comparable](s S, inplace bool) S
- func UniqueInt32s(slice []int32, inplace bool) []int32deprecated
- func UniqueInt64s(slice []int64, inplace bool) []int64deprecated
- func UniqueStrings(slice []string, inplace bool) []stringdeprecated
- func Values[M ~map[K]V, K comparable, V any](m M, filter ...func(K, V) bool) []V
- type IJ
- type Mapdeprecated
- func NewMap() Mapdeprecated
- type PanicError
- type Requestdeprecated
- type SafeMapdeprecated
- func NewSafeMap() *SafeMapdeprecated
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Caller ¶
Caller returns function name, filename, and the line number of the caller. The argument skip is the number of stack frames to ascend, with 0 identifying the caller of Caller.
func CallerName ¶
func CallerName() string
CallerName returns the function name of the direct caller. This is a convenient wrapper around Caller.
func ConvInts ¶
func ConvInts[T1, T2 constraints.Integer](slice []T1) []T2
ConvInts converts slice of integers of type T1 to a new slice of integers of type T2. The input data must be convertable to T2.
func Diff ¶
func Diff[S ~[]E, E comparable](slice S, others ...S) S
Diff allocates and returns a new slice which contains the values which present in slice, but not present in others.
If length of slice is zero, it returns nil.
func DiffInplace ¶
func DiffInplace[S ~[]E, E comparable](slice S, others ...S) S
DiffInplace returns a slice which contains the values which present' in slice, but not present in others. It does not allocate new memory, but modifies slice in-place.
If length of slice is zero, it returns nil.
func DiffInt32s
deprecated
func DiffInt64s
deprecated
func DiffMaps ¶
func DiffMaps[M ~map[K]V, K comparable, V any](m M, others ...M) M
DiffMaps returns a new map which contains elements which present in m, but not present in others.
If length of m is zero, it returns nil.
func DiffMapsInplace ¶
func DiffMapsInplace[M ~map[K]V, K comparable, V any](m M, others ...M) M
DiffMapsInplace removes elements that present in others from m.
func DiffStrings
deprecated
func DoRequest
deprecated
func EnsureError ¶
func EnsureError(v interface{}) error
EnsureError ensures the given value (should be non-nil) is an error. If it's not an error, `fmt.Errorf("%v", v)` will be used to convert it.
func Filter ¶
Filter iterates the given slices, it calls predicate(i, elem) for each elem in the slices and returns a new slice of elements for which predicate(i, elem) returns true.
func FilterInt32s
deprecated
FilterInt32s iterates the given slice, it calls predicate(i) for i in range [0, n), where n is the length of the slice. It returns a new slice of elements for which predicate(i) returns true.
Deprecated: the generic function Filter is favored over this.
func FilterInt64s
deprecated
FilterInt64s iterates the given slice, it calls predicate(i) for i in range [0, n), where n is the length of the slice. It returns a new slice of elements for which predicate(i) returns true.
Deprecated: the generic function Filter is favored over this.
func FilterMaps ¶
func FilterMaps[M ~map[K]V, K comparable, V any](predicate func(k K, v V) bool, maps ...M) M
FilterMaps iterates the given maps, it calls predicate(k, v) for each key value in the maps and returns a new map of key value pairs for which predicate(k, v) returns true.
func FilterStrings
deprecated
FilterStrings iterates the given slice, it calls predicate(i) for i in range [0, n), where n is the length of the slice. It returns a new slice of elements for which predicate(i) returns true.
Deprecated: the generic function Filter is favored over this.
func FormatInts ¶
func FormatInts[T constraints.Integer](slice []T, base int) []string
FormatInts converts slice of integers to a slice of strings. It returns nil if there is no element in given slice.
func GetOutboundIP ¶
GetOutboundIP returns the preferred outbound ip of this machine.
func IdentifyPanic ¶
func IdentifyPanic() string
IdentifyPanic reports the panic location when a panic happens.
func IndexFunc ¶
IndexFunc iterates the given slice, it calls predicate(i) for i in range [0, n) where n is the length of the slice. When predicate(i) returns true, it stops and returns the index i.
func IntKeys
deprecated
func IntKeys[M ~map[K]V, K constraints.Integer, V any](m M) (keys []int64)
IntKeys returns a int64 slice containing all the keys present in the map, in an indeterminate order.
Deprecated: the generic function Keys is favored over this.
func IntValues
deprecated
func IntValues[M ~map[K]V, K comparable, V constraints.Integer](m M) (values []int64)
IntValues returns a int64 slice containing all the values present in the map, in an indeterminate order.
Deprecated: the generic function Values is favored over this.
func JSON ¶
func JSON(v interface{}) string
JSON converts given object to a json string, it never returns error. The marshalling method used here does not escape HTML characters, and map keys are sorted, which helps human reading.
func Keys ¶
func Keys[M ~map[K]V, K comparable, V any](m M, filter ...func(K, V) bool) []K
Keys returns the keys of the map m. The keys will be in an indeterminate order.
Optionally, a filter function can be given to make it returning only keys for which filter(k, v) returns true.
func LastIndexFunc ¶
LastIndexFunc iterates the given slice, it calls predicate(i) for i in range [0, n) in descending order, where n is the length of the slice. When predicate(i) returns true, it stops and returns the index i.
func MapKeys
deprecated
func MapKeys[M ~map[K]V, K comparable, V any](m M) interface{}
MapKeys returns the keys of the map m. The keys will be in an indeterminate order.
Deprecated: the generic function Keys is favored over this.
func MapValues
deprecated
func MapValues[M ~map[K]V, K comparable, V any](m M) interface{}
MapValues returns the values of the map m. The values will be in an indeterminate order.
Deprecated: the generic function Values is favored over this.
func MatchGroups ¶
MatchGroups returns the matched named capturing groups. A returned value of nil indicates no match.
func MatchStringGroups ¶
MatchStringGroups returns the matched named capturing groups. A returned value of nil indicates no match.
func MergeMaps ¶
func MergeMaps[M ~map[K]V, K comparable, V any](maps ...M) M
MergeMaps returns a new map containing all key values present in given maps.
func MergeMapsTo ¶
func MergeMapsTo[M ~map[K]V, K comparable, V any](dst M, others ...M) M
MergeMapsTo adds key values present in others to the dst map.
func PanicOnError ¶
func PanicOnError(args ...interface{})
PanicOnError fires a panic if any of the args is non-nil error.
func ParseHTMLTemplates ¶
func ParseHTMLTemplates(rootDir string, rePattern string, funcMap htmltemplate.FuncMap) (*htmltemplate.Template, error)
ParseHTMLTemplates parses files under `rootDir` which matches the regular expression `rePattern`. Optionally a `funcMap` can be specified to use with the parsed templates.
The returned Template holds the parsed templates under the root directory, template can be retrieved using Template.Lookup(name), where name is the file path relative to rootDir, without leading "./".
func ParseInts ¶
func ParseInts[T constraints.Integer](slice []string, base int) []T
ParseInts converts slice of strings to a slice of integers. It returns nil if there is no element in given slice.
Note that if the input data contains non-integer strings, the errors returned from strconv.ParseInt are ignored, and the returned slice will have less elements than the input slice.
func ParseTextTemplates ¶
func ParseTextTemplates(rootDir string, rePattern string, funcMap texttemplate.FuncMap) (*texttemplate.Template, error)
ParseTextTemplates parses files under `rootDir` which matches the regular expression `rePattern`. Optionally a `funcMap` can be specified to use with the parsed templates.
The returned Template holds the parsed templates under the root directory, template can be retrieved using Template.Lookup(name), where name is the file path relative to rootDir, without leading "./".
func Pretty ¶
func Pretty(v interface{}) string
Pretty converts given object to a pretty formatted json string. If the input is a json string, it will be formatted using json.Indent with four space characters as indent.
func Pretty2 ¶
func Pretty2(v interface{}) string
Pretty2 is like Pretty, but it uses two space characters as indent, instead of four.
func Recover ¶
func Recover(f func(ctx context.Context, panicErr *PanicError)) func(ctx context.Context)
Recover returns a function which can be used to recover panics. It accepts a panicErr handler function which may be used to log the panic error and context information.
Note that the returned function should not be wrapped by another function, instead it should be called directly by the `defer` statement, else it won't work as you may expect.
func Reverse ¶
Reverse returns a slice of the elements in reversed order. When inplace is true, it does not allocate new memory, but the slice is reversed in place.
func ReverseInt32s
deprecated
ReverseInt32s returns a new slice of the elements in reversed order.
When inplace is true, the slice is reversed in place, it does not create a new slice, but returns the original slice with reversed order.
Deprecated: the generic function Reverse is favored over this.
func ReverseInt64s
deprecated
ReverseInt64s returns a new slice of the elements in reversed order.
When inplace is true, the slice is reversed in place, it does not create a new slice, but returns the original slice with reversed order.
Deprecated: the generic function Reverse is favored over this.
func ReverseStrings
deprecated
ReverseStrings returns a new slice of the elements in reversed order.
When inplace is true, the slice is reversed in place, it does not create a new slice, but returns the original slice with reversed order.
Deprecated: the generic function Reverse is favored over this.
func Safe ¶
func Safe(f func()) func() error
Safe returns an wrapped function with panic recover.
Note that if panic happens, the wrapped function does not log messages, instead it will be returned as a `*PanicError`, the caller take responsibility to log the panic messages.
func Safe1 ¶
Safe1 returns an wrapped function with panic recover.
Note that if panic or error happens, the wrapped function does not log messages, instead it will be returned as an error, the caller take responsibility to log the panic or error messages.
func SetDefault ¶
func SetDefault(dst interface{}, value ...interface{})
SetDefault checks whether dst points to a zero value, if yes, it sets the first non-zero value to dst. dst must be a pointer to same type as value, else it panics.
func SingleJoin ¶
SingleJoin joins the given text segments using sep. No matter whether a segment begins or ends with sep or not, it guarantees that only one sep appears between two segments.
func SlashJoin ¶
SlashJoin joins the given path segments using "/". No matter whether a segment begins or ends with "/" or not, it guarantees that only one "/" appears between two segments.
func Sort ¶
func Sort[S ~[]E, E constraints.Ordered](s S) S
Sort sorts the given slice ascending and returns it.
func SortDesc ¶
func SortDesc[S ~[]E, E constraints.Ordered](s S) S
SortDesc sorts the given slice descending and returns it.
func Split ¶
Split splits a large slice []T to batches, it returns a slice of type [][]T whose elements are sub slices of slice.
func SplitInt32s ¶
SplitInt32s splits a large int32 slice to batches.
func SplitInt64s ¶
SplitInt64s splits a large int64 slice to batches.
func SplitSlice
deprecated
func SplitStrings ¶
SplitStrings splits a large string slice to batches.
func StringKeys
deprecated
func StringValues
deprecated
func StringValues[M ~map[K]V, K comparable, V ~string](m M) (values []string)
StringValues returns a string slice containing all the values present in the map, in an indeterminate order.
Deprecated: the generic function Values is favored over this.
func Sum ¶
func Sum[T constraints.Integer](slice []T) int64
Sum returns the sum value of the elements in the given slice.
func ToHashSet ¶
func ToHashSet[S ~[]E, E comparable](slice S) map[E]bool
ToHashSet converts the given slice to a hash set, using elements from the slice as keys and true as values.
func ToInterfaceSlice ¶
func ToInterfaceSlice[S ~[]E, E any](slice S) []interface{}
ToInterfaceSlice returns a []interface{} containing elements from slice.
func ToMap ¶
func ToMap[S ~[]E, E any, K comparable, V any](slice S, f func(E) (K, V)) map[K]V
ToMap converts the given slice to a map, it calls f for each element in slice to get key values to construct the returned map.
func Unique ¶
func Unique[S ~[]E, E comparable](s S, inplace bool) S
Unique returns a slice containing the elements of the given slice in same order, but removes duplicate values. When inplace is true, it does not allocate new memory, the unique values will be written to the input slice from the beginning.
func UniqueInt32s
deprecated
UniqueInt32s returns a new slice containing the elements of the given slice in same order, but filter out duplicate values.
When inplace is true, it does not create a new slice, the unique values will be written to the input slice from the beginning.
Deprecated: the generic function Unique is favored over this.
func UniqueInt64s
deprecated
UniqueInt64s returns a new slice containing the elements of the given slice in same order, but filter out duplicate values.
When inplace is true, it does not create a new slice, the unique values will be written to the input slice from the beginning.
Deprecated: the generic function Unique is favored over this.
func UniqueStrings
deprecated
UniqueStrings returns a new slice containing the elements of the given slice in same order, but filter out duplicate values.
When inplace is true, it does not create a new slice, the unique values will be written to the input slice from the beginning.
Deprecated: the generic function Unique is favored over this.
func Values ¶
func Values[M ~map[K]V, K comparable, V any](m M, filter ...func(K, V) bool) []V
Values returns the values of the map m. The values will be in an indeterminate order.
Optionally, a filter function can be given to make it returning only values for which filter(k, v) returns true.
Types ¶
type IJ ¶
type IJ struct{ I, J int }
IJ represents a batch index of i, j.
func SplitBatch ¶
SplitBatch splits a large number to batch, it's mainly designed to help operations with large slice, such as inserting lots of records into database, or logging lots of identifiers, etc.
type Map
deprecated
Map is an alias name of gemap.Map.
Deprecated: please use gemap.Map directly, this alias name will be removed in future releases.
type PanicError ¶
PanicError represents an captured panic error.
func (*PanicError) Error ¶
func (p *PanicError) Error() string
type SafeMap
deprecated
SafeMap is an alias name of gemap.SafeMap.
Deprecated: please use gemap.SafeMap directly, this alias name will be removed in future releases.
func NewSafeMap
deprecated
func NewSafeMap() *SafeMap
NewSafeMap is an alias name of gemap.NewSafeMap.
Deprecated: please use gemap.NewSafeMap directly, this alias name will be removed in future releases.