Documentation
¶
Index ¶
- Constants
- Variables
- func CheckBySalt(check, hash, salt string) bool
- func CostTimeDur(fn func()) time.Duration
- func CountTotalPage(total int64, size int) int
- func DeepCopyByGob(src, dst any) error
- func DeepCopyByJson(src, dst any) error
- func DelRightRN(item string, r string, n int) string
- func DiffField[T comparable](o, n T) (t T)
- func GetLocalIpv4ByUdp() (ip string, err error)
- func GetLocalIpv4List() ([]string, error)
- func GetMacAddr() (map[string]string, error)
- func GetMapNestedValue(m map[string]any, keys ...string) (any, bool)
- func GetPubIpVipip(typ IpTyp) (ip string, err error)
- func GetPubIpVipsb(typ IpTyp) (ip string, err error)
- func HasNum(s string) bool
- func HmacHashWithSalt(ps, salt string) string
- func If[T any](condition bool, trueVal, falseVal T) T
- func IfEx[T any](boolExpr bool, trueReturn, falseReturn T) T
- func IfPtr[T any](condition bool, trueVal, falseVal T) *T
- func Ip2Long(str string) uint32
- func IsNilOrZero[T comparable](ptr *T) bool
- func ItemIsInSlice[T comparable](item T, slice []T) bool
- func JsonMarshal(v any) ([]byte, error)
- func JsonMarshalToString(v any) (string, error)
- func JsonMarshalToStringV2(v any) (string, error)
- func JsonMarshalV2(v any) ([]byte, error)
- func JsonUnmarshal(data []byte, v any) error
- func JsonUnmarshalFromString(str string, v any) error
- func JsonUnmarshalFromStringV2(str string, v any) error
- func JsonUnmarshalV2(data []byte, v any) error
- func MapClear[M ~map[K]V, K comparable, V any](m M)
- func MapClone[M ~map[K]V, K comparable, V any](m M) M
- func MapCopy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)
- func MapDeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)
- func MapEqual[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool
- func MapEqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool
- func MapKeys[M ~map[K]V, K comparable, V any](m M) []K
- func MapPtr2Value[K comparable, V any](m map[K]*V) map[K]V
- func MapToObj(m map[string]any, obj any) error
- func MapValue2Ptr[K comparable, V any](m map[K]V) map[K]*V
- func MapValues[M ~map[K]V, K comparable, V any](m M) []V
- func NewGoogleUUID() string
- func ObjToMap(obj any, m *map[string]any) error
- func PanicIfErr(err error)
- func PanicToErr(fn func()) (err error)
- func ParseDateStr(dateStr string) (time.Time, error)
- func PathExists(path string) (bool, error)
- func Ptr2Value[T any](ptr *T) T
- func Ptr2ValueWithDefault[T any](ptr *T, defaultVal T) T
- func PtrOrNil[T comparable](v T) *T
- func RandBytesMask(n int) []byte
- func RandCodeMask(n int) string
- func RandIdMask(n int) string
- func RandSelfDefMask(n int, bs string) string
- func RandStringMask(n int) string
- func RemoteIp(req *http.Request) string
- func RemoveInvalidParentheses(s string, pair [2]rune) string
- func ReplaceLast(s, old, new string) string
- func RetryFn(cfg RetryConfig, fn func() error) (err error)
- func RuneIndex(src, sub []rune) int
- func SliceEqual[T comparable](s1, s2 []T) bool
- func SliceGroupsOf[T any](arr []T, num int64) [][]T
- func SliceIsInSlice[T comparable](item []T, slice []T) bool
- func SlicePtr2Value[T any](s []*T) []T
- func SliceSub[T comparable](s1 []T, s2 []T) []T
- func SliceUnique[T comparable](arr []T) []T
- func SliceValue2Ptr[T any](s []T) []*T
- func StringsContainsSlice(s string, arr []string) bool
- func TlsCertGenerateToFile(path string) error
- func TlsCertGenerateToMap() (cert map[string]string, err error)
- func ValidateEmail(email string) bool
- func ValidatePhone(phone string) bool
- func Value2Ptr[T any](v T) *T
- type IpTyp
- type Quit
- type Rate
- type RetryConfig
- type WorkerPool
- func (p *WorkerPool) Pause(ctx context.Context)
- func (p *WorkerPool) Size() int
- func (p *WorkerPool) Stop()
- func (p *WorkerPool) StopWait()
- func (p *WorkerPool) Stopped() bool
- func (p *WorkerPool) Submit(task func())
- func (p *WorkerPool) SubmitBlockBySize(task func())
- func (p *WorkerPool) SubmitWait(task func())
- func (p *WorkerPool) WaitingQueueSize() int
Constants ¶
const ( IPSB4URL = `http://api-ipv4.ip.sb/ip` IPSB6URL = `http://api-ipv6.ip.sb/ip` )
const ( XForwardedFor = "X-Forwarded-For" XRealIP = "X-Real-IP" )
const (
IPIP4URL = `http://myip.ipip.net/s`
)
Variables ¶
var ( CertName = "cert.pem" KeyName = "key.pem" )
var HttpClient = &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, DisableKeepAlives: true, }, }
InsecureSkipVerify DisableKeepAlives true
Functions ¶
func CheckBySalt ¶
CheckBySalt check HmacHashWithSalt
func CountTotalPage ¶ added in v1.1.6
CountTotalPage 计算总页数
func DelRightRN ¶ added in v1.1.3
DelRightRN 移除字符串 item 中最后 n 个特定字符 r,并返回修改后的字符串。 如果 item 中 r 出现的次数小于 n,则不进行修改。 参数:
item: 要处理的字符串 r: 要移除的特定字符 n: 要移除的 r 的数量
返回值:
修改后的字符串
func GetLocalIpv4ByUdp ¶
GetLocalIpv4ByUdp 获取 localIp by net.Dial("udp", "8.8.8.8:53")
func GetMacAddr ¶ added in v1.1.0
GetMacAddr 获取本机的MAC地址
func GetMapNestedValue ¶ added in v1.1.11
获取深层 value 的通用函数
func GetPubIpVipip ¶ added in v1.1.0
GetPubIpVipip 通过ip.sb获取公网ip 仅支持ipv4
func GetPubIpVipsb ¶ added in v1.1.0
GetPubIpVipsb 通过ip.sb获取公网ip
func HmacHashWithSalt ¶ added in v1.1.0
HmacHashWithSalt hmac sha2 and salt make hash
func If ¶ added in v1.2.0
If returns trueVal if condition is true, otherwise falseVal. 场景:由行三元运算符: vege.If(age > 18, "Adult", "Minor")
func IsNilOrZero ¶ added in v1.2.0
func IsNilOrZero[T comparable](ptr *T) bool
IsNilOrZero checks if a pointer is nil OR if it points to a zero value.
func ItemIsInSlice ¶
func ItemIsInSlice[T comparable](item T, slice []T) bool
func JsonMarshal ¶
func JsonMarshalToString ¶
func JsonMarshalToStringV2 ¶
func JsonMarshalV2 ¶
func JsonUnmarshal ¶
func JsonUnmarshalFromString ¶
func JsonUnmarshalV2 ¶
func MapClear ¶
func MapClear[M ~map[K]V, K comparable, V any](m M)
MapClear removes all entries from m, leaving it empty.
func MapClone ¶
func MapClone[M ~map[K]V, K comparable, V any](m M) M
MapClone returns a copy of m. This is a shallow clone: the new keys and values are set using ordinary assignment.
func MapCopy ¶
func MapCopy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)
MapCopy copies all key/value pairs in src adding them to dst. When a key in src is already present in dst, the value in dst will be overwritten by the value associated with the key in src.
func MapDeleteFunc ¶
func MapDeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)
MapDeleteFunc deletes any key/value pairs from m for which del returns true.
func MapEqual ¶
func MapEqual[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool
MapEqual reports whether two maps contain the same key/value pairs. MapValues are compared using ==.
func MapEqualFunc ¶
func MapEqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool
MapEqualFunc is like MapEqual, but compares values using eq. MapKeys are still compared with ==.
func MapKeys ¶
func MapKeys[M ~map[K]V, K comparable, V any](m M) []K
MapKeys returns the keys of the map m. The keys will be in an indeterminate order.
func MapPtr2Value ¶ added in v1.2.0
func MapPtr2Value[K comparable, V any](m map[K]*V) map[K]V
MapPtr2Value converts map[K]*V to map[K]V.
func MapValue2Ptr ¶ added in v1.2.0
func MapValue2Ptr[K comparable, V any](m map[K]V) map[K]*V
MapValue2Ptr converts map[K]V to map[K]*V.
func MapValues ¶
func MapValues[M ~map[K]V, K comparable, V any](m M) []V
MapValues returns the values of the map m. The values will be in an indeterminate order.
func PanicIfErr ¶
func PanicIfErr(err error)
func PanicToErr ¶
func PanicToErr(fn func()) (err error)
func ParseDateStr ¶ added in v1.1.11
ParseDateStr 解析常见的日期格式字符串到 time.Time
func Ptr2Value ¶
func Ptr2Value[T any](ptr *T) T
Ptr2Value returns ptr's value; if ptr==nil, return zero value of T.
func Ptr2ValueWithDefault ¶ added in v1.2.0
func Ptr2ValueWithDefault[T any](ptr *T, defaultVal T) T
Ptr2ValueWithDefault returns ptr's value; if ptr==nil, return the specific defaultVal. 场景:如果指针为空,我想返回 -1 或者 "unknown",而不是 0 或 ""。
func PtrOrNil ¶ added in v1.2.0
func PtrOrNil[T comparable](v T) *T
PtrOrNil returns nil if v is the zero value of T, otherwise returns &v. 场景:在更新数据库时,如果字段是零值(如空字符串),不想传空指针,而是传 nil 以避免覆盖。 注意:T 必须是可比较类型 (comparable)。
func RandBytesMask ¶
func RandCodeMask ¶
func RandIdMask ¶
func RandSelfDefMask ¶
func RandStringMask ¶
func RemoveInvalidParentheses ¶
RemoveInvalidParentheses 去除s中不成对的括号,pair为成对的括号
func ReplaceLast ¶ added in v1.1.4
ReplaceLast 替换字符串最后一个匹配项
func RetryFn ¶ added in v1.1.11
func RetryFn(cfg RetryConfig, fn func() error) (err error)
func SliceEqual ¶
func SliceEqual[T comparable](s1, s2 []T) bool
func SliceGroupsOf ¶
func SliceIsInSlice ¶
func SliceIsInSlice[T comparable](item []T, slice []T) bool
func SlicePtr2Value ¶ added in v1.2.0
func SlicePtr2Value[T any](s []*T) []T
SlicePtr2Value converts a slice of pointers to a slice of values. Nil pointers in the slice will be converted to zero values. 场景:处理 API 响应,将 []*User 转为 []User。
func SliceSub ¶
func SliceSub[T comparable](s1 []T, s2 []T) []T
func SliceUnique ¶
func SliceUnique[T comparable](arr []T) []T
func SliceValue2Ptr ¶ added in v1.2.0
func SliceValue2Ptr[T any](s []T) []*T
SliceValue2Ptr converts a slice of values to a slice of pointers. 场景:批量构造数据库模型或 API 请求体。
func StringsContainsSlice ¶ added in v1.1.4
StringsContainsSlice 展开arr判断s是否Contains
func TlsCertGenerateToFile ¶
TlsCertGenerateToFile 生成自签tls证书 cert.pem key.pem 到指定文件夹中
func TlsCertGenerateToMap ¶
TlsCertGenerateToMap 生成自签tls证书 cert.pem key.pem 到map
func ValidateEmail ¶ added in v1.1.11
func ValidatePhone ¶ added in v1.1.11
Types ¶
type Quit ¶
type Quit struct {
// contains filtered or unexported fields
}
func (*Quit) WaitCloseWithFn ¶
func (q *Quit) WaitCloseWithFn(fn func())
type RetryConfig ¶ added in v1.1.11
type WorkerPool ¶
type WorkerPool struct {
// contains filtered or unexported fields
}
WorkerPool is a collection of goroutines, where the number of concurrent goroutines processing requests does not exceed the specified maximum.
func NewWorkerPool ¶
func NewWorkerPool(maxWorkers int) *WorkerPool
NewWorkerPool creates and starts a pool of worker goroutines.
The maxWorkers parameter specifies the maximum number of workers that can execute tasks concurrently. When there are no incoming tasks, workers are gradually stopped until there are no remaining workers.
func (*WorkerPool) Pause ¶
func (p *WorkerPool) Pause(ctx context.Context)
Pause causes all workers to wait on the given Context, thereby making them unavailable to run tasks. Pause returns when all workers are waiting. Tasks can continue to be queued to the workerpool, but are not executed until the Context is canceled or times out.
Calling Pause when the worker pool is already paused causes Pause to wait until all previous pauses are canceled. This allows a goroutine to take control of pausing and unpausing the pool as soon as other goroutines have unpaused it.
When the workerpool is stopped, workers are unpaused and queued tasks are executed during StopWait.
func (*WorkerPool) Size ¶
func (p *WorkerPool) Size() int
Size returns the maximum number of concurrent workers.
func (*WorkerPool) Stop ¶
func (p *WorkerPool) Stop()
Stop stops the worker pool and waits for only currently running tasks to complete. Pending tasks that are not currently running are abandoned. Tasks must not be submitted to the worker pool after calling stop.
Since creating the worker pool starts at least one goroutine, for the dispatcher, Stop() or StopWait() should be called when the worker pool is no longer needed.
func (*WorkerPool) StopWait ¶
func (p *WorkerPool) StopWait()
StopWait stops the worker pool and waits for all queued tasks tasks to complete. No additional tasks may be submitted, but all pending tasks are executed by workers before this function returns.
func (*WorkerPool) Stopped ¶
func (p *WorkerPool) Stopped() bool
Stopped returns true if this worker pool has been stopped.
func (*WorkerPool) Submit ¶
func (p *WorkerPool) Submit(task func())
Submit enqueues a function for a worker to execute.
Any external values needed by the task function must be captured in a closure. Any return values should be returned over a channel that is captured in the task function closure.
Submit will not block regardless of the number of tasks submitted. Each task is immediately given to an available worker or to a newly started worker. If there are no available workers, and the maximum number of workers are already created, then the task is put onto a waiting queue.
When there are tasks on the waiting queue, any additional new tasks are put on the waiting queue. Tasks are removed from the waiting queue as workers become available.
As long as no new tasks arrive, one available worker is shutdown each time period until there are no more idle workers. Since the time to start new goroutines is not significant, there is no need to retain idle workers indefinitely.
func (*WorkerPool) SubmitBlockBySize ¶
func (p *WorkerPool) SubmitBlockBySize(task func())
when queue length equal maximum block
func (*WorkerPool) SubmitWait ¶
func (p *WorkerPool) SubmitWait(task func())
SubmitWait enqueues the given function and waits for it to be executed.
func (*WorkerPool) WaitingQueueSize ¶
func (p *WorkerPool) WaitingQueueSize() int
WaitingQueueSize returns the count of tasks in the waiting queue.