hutils

package module
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: MIT Imports: 21 Imported by: 0

README

hutils

不使用第三方库,不产生额外依赖

为了尽可能减少依赖,一些有用的代码贴在这里

import "golang.org/x/crypto/pbkdf2"

// 密码加密
func EncodePassword(password, salt string) string {
	dk := pbkdf2.Key([]byte(password), []byte(salt), 4096, 32, sha256.New)
	return hex.EncodeToString(dk)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefTimeUtil = NewTimeUtil()
)

Functions

func AESDecrypt added in v0.3.3

func AESDecrypt(src []byte, key []byte) ([]byte, error)

func AESEncrypt added in v0.3.3

func AESEncrypt(src []byte, key []byte) ([]byte, error)

func AppendBytes added in v0.0.2

func AppendBytes(fpath string, bs []byte) error

func Clamp added in v0.6.6

func Clamp[T cmp.Ordered](v, min, max T) T

func FormatTime added in v0.1.3

func FormatTime(t time.Time) string

func MD5 added in v0.1.5

func MD5(s string) string

func MapToSlice added in v0.6.3

func MapToSlice[K comparable, V, T any](m map[K]V, transFunc func(K, V) (T, bool)) []T

func MapToSliceK added in v0.6.1

func MapToSliceK[K comparable, V any](m map[K]V) []K

func MapToSliceV added in v0.6.1

func MapToSliceV[K comparable, V any](m map[K]V) []V

func Must

func Must(i interface{}, e error) interface{}

func Now added in v0.1.3

func Now() time.Time

func RandMapK added in v0.6.0

func RandMapK[K comparable, V any](m map[K]V, n int) []K

func RandMapV added in v0.6.0

func RandMapV[K comparable, V any](m map[K]V, n int) []V

func RandSlice added in v0.5.0

func RandSlice[T any](slice []T, n int) []T

func RandSliceWeightFunc added in v0.6.2

func RandSliceWeightFunc[T any](slice []T, weightFunc func(T) int) T

func RandSliceWeightFuncN added in v0.6.2

func RandSliceWeightFuncN[T any](slice []T, n int, weightFunc func(T) int) []T

RandSliceWeightFuncN 根据权重随机选择 n 个元素,权重由 weightFunc 提供,可以重复选择同一个元素

func RandString added in v0.1.5

func RandString(n int) string

func ReadJson

func ReadJson(fpath string, i interface{}) error

func Recover added in v0.1.1

func Recover()

func RecoverF added in v0.4.0

func RecoverF(f func(r interface{}))

func SliceToMap added in v0.6.1

func SliceToMap[K comparable, V, T any](a []V, transFunc func(V) (K, T, bool)) map[K]T

func SliceToSlice added in v0.6.5

func SliceToSlice[T, V any](a []T, transFunc func(T) (V, bool)) []V

func TopMapN added in v0.6.0

func TopMapN[K comparable, V any](m map[K]V, n int, less func(V, V) bool) []V

func TopSliceN added in v0.6.0

func TopSliceN[T any](a []T, n int, less func(T, T) bool) []T

func WaitExitSignal added in v0.4.0

func WaitExitSignal()

func WaitTimeoutFunc added in v0.2.1

func WaitTimeoutFunc(timeout time.Duration, f func()) bool

func WaitUntil added in v0.3.2

func WaitUntil(timeout time.Duration, f func() bool) bool

func WriteBytes added in v0.0.2

func WriteBytes(fpath string, data []byte) error

func WriteJson added in v0.0.2

func WriteJson(fpath string, i interface{}) error

Types

type Duration added in v0.4.0

type Duration struct {
	time.Duration
}

func (*Duration) MarshalJSON added in v0.4.0

func (d *Duration) MarshalJSON() ([]byte, error)

func (*Duration) UnmarshalJSON added in v0.4.0

func (d *Duration) UnmarshalJSON(b []byte) (err error)

type MysqlDSNCreator added in v0.4.0

type MysqlDSNCreator struct {
	// contains filtered or unexported fields
}

func NewMysqlDSNCreator added in v0.4.0

func NewMysqlDSNCreator(user, password, host, port, dbName string) *MysqlDSNCreator

func (*MysqlDSNCreator) DataSourceName added in v0.4.0

func (opts *MysqlDSNCreator) DataSourceName() string

func (*MysqlDSNCreator) SetCharset added in v0.4.0

func (opts *MysqlDSNCreator) SetCharset(charset string) *MysqlDSNCreator

func (*MysqlDSNCreator) SetLoc added in v0.4.0

func (opts *MysqlDSNCreator) SetLoc(loc string) *MysqlDSNCreator

func (*MysqlDSNCreator) SetParseTime added in v0.4.0

func (opts *MysqlDSNCreator) SetParseTime(parseTime bool) *MysqlDSNCreator

func (*MysqlDSNCreator) SetReadTimeout added in v0.4.0

func (opts *MysqlDSNCreator) SetReadTimeout(readTimeout time.Duration) *MysqlDSNCreator

func (*MysqlDSNCreator) SetTimeout added in v0.4.0

func (opts *MysqlDSNCreator) SetTimeout(timeout time.Duration) *MysqlDSNCreator

func (*MysqlDSNCreator) SetWriteTimeout added in v0.4.0

func (opts *MysqlDSNCreator) SetWriteTimeout(writeTimeout time.Duration) *MysqlDSNCreator

type TimeUtil added in v0.1.3

type TimeUtil struct {
	// contains filtered or unexported fields
}

时间工具,在测试中,很多时候需要控制 time.Now() 产生的时间 例如:设置当前时间为 3天 后,用正常时间的 1/10 行进时间

func NewTimeUtil added in v0.1.3

func NewTimeUtil() *TimeUtil

func (*TimeUtil) Add added in v0.1.3

func (t *TimeUtil) Add(duration time.Duration)

func (*TimeUtil) Now added in v0.1.3

func (t *TimeUtil) Now() time.Time

func (*TimeUtil) SetDebug added in v0.1.3

func (t *TimeUtil) SetDebug(isDebug bool)

func (*TimeUtil) SetNow added in v0.1.3

func (t *TimeUtil) SetNow(now time.Time)

type WaitTimeout added in v0.2.1

type WaitTimeout struct {
	// contains filtered or unexported fields
}

WaitTimeout 带有超时的 WaitGroup,避免了 wg.Wait() 可能引起的 blocked forever,协程永远无法被释放

func NewWaitTimeout added in v0.2.1

func NewWaitTimeout() *WaitTimeout

NewWaitTimeout 必须使用 NewWaitTimeout 实例化 WaitTimeout

func (*WaitTimeout) Add added in v0.2.1

func (wt *WaitTimeout) Add(n int32) *WaitTimeout

func (*WaitTimeout) Done added in v0.2.1

func (wt *WaitTimeout) Done()

func (*WaitTimeout) Wait added in v0.2.1

func (wt *WaitTimeout) Wait(timeout time.Duration) chan bool

Wait, 等待过程外置,外部使用时,可以先获取 chan, 在释放完可能存在的锁后,进入 <-chan 环节,避免等待时间内持有锁 一个 WaitTimeout 只能使用一次,wt.Done() 后会 close(wt.doneChan),再次 wt.Done() 会触发 panic

type Watcher added in v0.2.2

type Watcher struct {
	// contains filtered or unexported fields
}

func NewWatcher added in v0.2.2

func NewWatcher() *Watcher

func (*Watcher) Notify added in v0.2.2

func (w *Watcher) Notify()

func (*Watcher) Watch added in v0.2.2

func (w *Watcher) Watch(timeout time.Duration) chan bool

Jump to

Keyboard shortcuts

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