hutils

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 19 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 FormatTime added in v0.1.3

func FormatTime(t time.Time) string

func InitRandSeed added in v0.1.7

func InitRandSeed()

func MD5 added in v0.1.5

func MD5(s string) string

func Must

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

func Now added in v0.1.3

func Now() time.Time

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 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