gtools

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2024 License: MIT Imports: 63 Imported by: 3

README

gtools

工具包

GitHub releaselicense

简体中文 | English

安装

go get github.com/jeffcail/gtools

包元素

邮件发送 163邮箱、QQ邮箱、126邮箱、Gmail邮箱
加密 md5、sh1、文件md5、aes
jwt
map
redis
gorm 动态搜索+分页
xorm
oss 七牛云文
mongodb 单个写入、批量写入、修改、统计、删除
etcd 写入、读取、修改、删除
敏感词过滤
切片
参数校验
限流 计数器、漏桶算法、令牌桶算法
基于内存的缓存系统
定时监听器
api统一返回格式
http请求 POST、GET
常见正则校验
使用Lua操作redis
解决TCP粘包拆包问题
leveldb
viper

Documentation

Index

Constants

View Source
const MsgHeader = "12345678"

Variables

View Source
var (
	ErrEmptyKey = errors.New("key could not be empty")
)
View Source
var Response *result
View Source
var ResponsePage *pageList
View Source
var (
	Wf *wordsfilter.WordsFilter
)

Functions

func CalculateCurrentTimeAndZeroTime

func CalculateCurrentTimeAndZeroTime() time.Duration

CalculateCurrentTimeAndZeroTime 计算当前时间和零点的时间差

func CompactStr added in v0.0.4

func CompactStr(str ...string) string

CompactStr compact more string

func ContentFilter

func ContentFilter(text string) bool

ContentFilter 需要过滤的地方,调用此方法,将要过滤的内容传递进去即可

func CurrentDayEnd

func CurrentDayEnd() string

CurrentDayEnd 获取当天结束时间

func CurrentDayZero

func CurrentDayZero() string

CurrentDayZero 获取当天零点时间

func Decode

func Decode(bytesBuffer io.Reader) (bodyBuf []byte, err error)

func EnValidateParam

func EnValidateParam(param interface{}) string

EnValidateParam validate params english

func Encode

func Encode(bytesBuffer io.Writer, content string) error

func FileMd5

func FileMd5(filepath string) (string, error)

func FilterString

func FilterString(key, value, operator string) func(db *gorm.DB) *gorm.DB

FilterString 进行快速条件过滤

func Get

func Get(url string, header map[string]string, params map[string]interface{}) ([]byte, error)

func InOrNotInFilter

func InOrNotInFilter(key string, value interface{}, operator string) func(db *gorm.DB) *gorm.DB

InOrNotInFilter where in 或者 where not in

func LoadConfig added in v0.0.2

func LoadConfig(cf string)

LoadConfig load config file

func Md5S

func Md5S(str string) string

func MergeMap

func MergeMap(dest, src map[interface{}]interface{}) map[interface{}]interface{}

MergeMap desc: 合并源 map 到目标 map

func NewClusterEtcd

func NewClusterEtcd(addr []string) (*clientv3.Client, error)

func NewEtcd

func NewEtcd(addr string) (*clientv3.Client, error)

func NewGorm

func NewGorm(dsn string, args ...[]int) (*gorm.DB, error)

NewGorm gorm 初始化mysql连接 eg: root:123456@tcp(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local

func NewLogger added in v0.0.4

func NewLogger(fileName string, maxSize int, maxAge int, localTime, compress bool) *zap.Logger

func NewRedis

func NewRedis(addr, password string, db int) (*redis.Client, error)

NewRedis 初始化redis 连接

func NewTicker

func NewTicker(ttl time.Duration, sch chan string, fn func(sch chan string) int)

func NewXrm

func NewXrm(dsn string, showSql bool, args ...[]int) (*xorm.Engine, error)

NewXrm 初始化mysql连接

func PKCS5Adding

func PKCS5Adding(ciphertext []byte, blockSize int) []byte

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

func Paginate

func Paginate(page interface{}, pageSize interface{}, s map[string]interface{}) func(db *gorm.DB) *gorm.DB

Paginate page, pageSize 传入参数 s 分页默认配置

func Post

func Post(url string, header map[string]string, param map[string]interface{}) ([]byte, error)

func PostMultipart

func PostMultipart(url string, header map[string]string, payload *bytes.Buffer) ([]byte, error)

func PreviousAfterDate

func PreviousAfterDate(n int) string

PreviousAfterDate 获取前后指定的时间日期

func PreviousAfterTime

func PreviousAfterTime(n int) string

PreviousAfterTime 获取前后指定的日期的当前时间

func PreviousDayEndTime

func PreviousDayEndTime() string

PreviousDayEndTime 获取前一天结束时间

func PreviousDayStartTime

func PreviousDayStartTime() string

PreviousDayStartTime 获取前一天零点时间

func PrintConfigMap added in v0.0.2

func PrintConfigMap(config map[string]interface{})

func QiNiuFileUpload

func QiNiuFileUpload(r *http.Request, store string) (string, error)

QiNiuFileUpload 上传文件到七牛云

func QiNiuResumeUploadFile

func QiNiuResumeUploadFile(r *http.Request, store string) (string, error)

QiNiuResumeUploadFile 七牛云文件断点续传

func QiNiuUploadChunk

func QiNiuUploadChunk(r *http.Request, store string) (string, string, error)

QiNiuUploadChunk 七牛云分片上传

func SendGmail

func SendGmail(from string, to []string, content []byte, mailServer, mailServerPort, mailPassword string) (err error)

SendGmail 发送谷歌邮件 支持同时给多人发送

func SendGmailEmail

func SendGmailEmail(from, to, title, content, mailServer, mailServerPort, mailPassword string) (err error)

SendGmailEmail 发送谷歌邮件 支持 Gmail邮箱 from 发件人邮箱 `xxxx@gmail.com` to 接受人邮箱 title 邮箱标题 content 邮箱内容 mailServer Gmail邮箱服务地址 `smtp.gmail.com` mailServerPort Gmail邮箱端口 `:587` mailPassword Gmail邮箱登陆密码

func SendMail

func SendMail(from, to, title, content, mailServer, mailServerPort, mailPassword string) (err error)

SendMail 发送邮件 支持 163邮箱、QQ邮箱、126邮箱 from 发件人邮箱 `xxxx@163.com`、`xxxx@qq.com`、`xxxx@126.com` to 接受人邮箱 title 邮箱标题 content 邮箱内容 mailServer 163邮箱服务地址 163: `smtp.163.com`、 QQ邮箱: `smtp.qq.com`、 126邮箱: `smtp.126.com` mailServerPort 163邮箱端口 163: `:465` 、 QQ邮箱端口: `:465`、 126邮箱: `:465` mailPassword 163邮箱、QQ邮箱、126邮箱 授权码 `xxxxxxxxxxx`

func SetWordsFilter

func SetWordsFilter()

SetWordsFilter 项目启动的时候,通过此方法将敏感词库加载进去

func Sha1

func Sha1(str string) string

func ToString added in v0.0.3

func ToString(i interface{}) (string, error)

func ZhValidateParam

func ZhValidateParam(param interface{}) string

ZhValidateParam validate params english

Types

type AesEncrypt

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

func NewAesEncryptInstance

func NewAesEncryptInstance(key string, secret string) (*AesEncrypt, error)

func (*AesEncrypt) AesBase64Decrypt

func (a *AesEncrypt) AesBase64Decrypt(b string) (string, error)

AesBase64Decrypt aes Base64 解密

func (*AesEncrypt) AesBase64Encrypt

func (a *AesEncrypt) AesBase64Encrypt(in string) (string, error)

AesBase64Encrypt aes Base64 加密

type Counter

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

func (*Counter) Pass

func (c *Counter) Pass() bool

func (*Counter) Reset

func (c *Counter) Reset(t time.Time)

func (*Counter) Set

func (c *Counter) Set(r int, tt time.Duration)

type Database

type Database interface {
	Put(key string, value interface{}) error
	Get(key string) ([]byte, error)
	Has(key string) (bool, error)
	Delete(key string) error
	SelectAll() iterator.Iterator
	SelectPrefixSubsetKeyAll(key string) ([]map[string]interface{}, error)
	CountPrefixSubsetKey(key string) (int64, error)
	CountAll() (int64, error)
	DeletePrefixSubsetKey(key string) (bool, error)
}

type EtcdS

type EtcdS struct{}

func (*EtcdS) Delete

func (w *EtcdS) Delete(client *clientv3.Client, key string) (err error)

func (*EtcdS) Get

func (w *EtcdS) Get(client *clientv3.Client, key string) (string, error)

func (*EtcdS) Put

func (w *EtcdS) Put(client *clientv3.Client, key, value string) (err error)

func (*EtcdS) Update

func (w *EtcdS) Update(client *clientv3.Client, key, value string) (err error)

type GMap

type GMap struct {
	M map[string]any
	// contains filtered or unexported fields
}
var Gm *GMap

func (*GMap) Delete

func (g *GMap) Delete(key string)

func (*GMap) Get

func (g *GMap) Get(key string) interface{}

func (*GMap) Has

func (g *GMap) Has(key string) bool

func (*GMap) Set

func (g *GMap) Set(key string, value interface{})

type GoRegs

type GoRegs struct{}

func NewGoRegs

func NewGoRegs() *GoRegs

func (*GoRegs) MatchChineseCharacter

func (g *GoRegs) MatchChineseCharacter(str string) bool

MatchChineseCharacter 纯汉字

func (*GoRegs) MatchChineseIDCardNumber

func (g *GoRegs) MatchChineseIDCardNumber(id string) bool

MatchChineseIDCardNumber 验证大陆身份证号

func (*GoRegs) MatchChinesePhoneNumber

func (g *GoRegs) MatchChinesePhoneNumber(str string) bool

MatchChinesePhoneNumber 大陆手机号

func (*GoRegs) MatchContainChineseCharacter

func (g *GoRegs) MatchContainChineseCharacter(str string) bool

MatchContainChineseCharacter 大陆手机号

func (*GoRegs) MatchDoubleByte

func (g *GoRegs) MatchDoubleByte(input string) bool

MatchDoubleByte 匹配双字节字符(包括汉字在内)

func (*GoRegs) MatchEmail

func (g *GoRegs) MatchEmail(str string) bool

MatchEmail email

func (*GoRegs) MatchEmptyLine

func (g *GoRegs) MatchEmptyLine(input string) bool

MatchEmptyLine 匹配零个或多个空白字符(包括空格、制表符、换页符等)

func (*GoRegs) MatchEnCharacter

func (g *GoRegs) MatchEnCharacter(str string) bool

MatchEnCharacter 纯英文字符串,大小写不敏感

func (*GoRegs) MatchGeNNumber

func (g *GoRegs) MatchGeNNumber(str string, n int) bool

MatchGeNNumber 长度不小于n位的纯数字

func (*GoRegs) MatchIPv4

func (g *GoRegs) MatchIPv4(input string) bool

MatchIPv4 ipv4

func (*GoRegs) MatchIPv6

func (g *GoRegs) MatchIPv6(input string) bool

MatchIPv6 ipv6

func (*GoRegs) MatchIntOrFloat

func (g *GoRegs) MatchIntOrFloat(str string) bool

MatchIntOrFloat 整数或者小数

func (*GoRegs) MatchIsContainSpecialCharacter

func (g *GoRegs) MatchIsContainSpecialCharacter(str string) bool

MatchIsContainSpecialCharacter 验证是否包含特殊字符串

func (*GoRegs) MatchLenNNumber

func (g *GoRegs) MatchLenNNumber(str string, n int) bool

MatchLenNNumber 长度为n的纯数字

func (*GoRegs) MatchLowerEnCharacter

func (g *GoRegs) MatchLowerEnCharacter(str string) bool

MatchLowerEnCharacter 纯小写英文字符串

func (*GoRegs) MatchMKNoContainSpecialCharacter

func (g *GoRegs) MatchMKNoContainSpecialCharacter(str string, m, k int) bool

MatchMKNoContainSpecialCharacter 长度m-k的字符串,包含数字、字母、汉字,不包含特殊字符 一般多用于校验用户名或昵称等

func (*GoRegs) MatchMNIntervalNumber

func (g *GoRegs) MatchMNIntervalNumber(str string, m, n int) bool

MatchMNIntervalNumber 长度m~n位的纯数字

func (*GoRegs) MatchMNLeCharacter

func (g *GoRegs) MatchMNLeCharacter(str string, m, z int) bool

MatchMNLeCharacter 长度m-z的字符串,包含特殊字符、数字、字母、汉字, 一般多用于校验用户名或昵称等

func (*GoRegs) MatchMNNovelsOfRealNumber

func (g *GoRegs) MatchMNNovelsOfRealNumber(str string, m, n int) bool

MatchMNNovelsOfRealNumber m~n位小数的正实数

func (*GoRegs) MatchNLeCharacter

func (g *GoRegs) MatchNLeCharacter(str string, n int) bool

MatchNLeCharacter 长度为n的字符,特殊字符除外

func (*GoRegs) MatchNNovelsOfRealNumber

func (g *GoRegs) MatchNNovelsOfRealNumber(str string, n int) bool

MatchNNovelsOfRealNumber 有n位小数的正实数

func (*GoRegs) MatchNanZeroNegNumber

func (g *GoRegs) MatchNanZeroNegNumber(str string) bool

MatchNanZeroNegNumber 非零的负整数

func (*GoRegs) MatchNanZeroNumber

func (g *GoRegs) MatchNanZeroNumber(str string) bool

MatchNanZeroNumber 非零的正整数

func (*GoRegs) MatchNumber

func (g *GoRegs) MatchNumber(str string) bool

MatchNumber 纯数字

func (*GoRegs) MatchNumberEnCharacter

func (g *GoRegs) MatchNumberEnCharacter(str string) bool

MatchNumberEnCharacter 数字和26个英文字母组成的字符串,大小写不敏感

func (*GoRegs) MatchNumberEnUnderscores

func (g *GoRegs) MatchNumberEnUnderscores(str string) bool

MatchNumberEnUnderscores 数字和26个英文字母组成的字符串,大小写不敏感

func (*GoRegs) MatchPass1

func (g *GoRegs) MatchPass1(str string, m, n int) bool

MatchPass1 密码1 由数字、26个英文字母或者下划线组成的英文开头的字符串, 长度m~n位

func (*GoRegs) MatchPass2

func (g *GoRegs) MatchPass2(str string) bool

MatchPass2 密码2 密码长度至少为8个字符。 包含至少一个小写字母。 包含至少一个大写字母。 包含至少一个数字。 包含至少一个特殊字符(例如 !@#$%^&*() 等

func (*GoRegs) MatchStartingWithNonZero

func (g *GoRegs) MatchStartingWithNonZero(str string) bool

MatchStartingWithNonZero 非零开头的纯数字

func (*GoRegs) MatchUpEnCharacter

func (g *GoRegs) MatchUpEnCharacter(str string) bool

MatchUpEnCharacter 纯大写英文字符串

type JwtClaims

type JwtClaims struct {
	Expire    time.Duration
	Secret    string
	LoginInfo interface{} `json:"login_info"`
	jwt.StandardClaims
}

func NewJwt

func NewJwt(expire time.Duration, secret string) *JwtClaims

func (*JwtClaims) GenerateToken

func (j *JwtClaims) GenerateToken(data interface{}) (string, error)

GenerateToken 签发Token

func (*JwtClaims) ParseToken

func (j *JwtClaims) ParseToken(token string) (*JwtClaims, error)

ParseToken 解析TOKEN

type LeakyBucket

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

func (*LeakyBucket) Pass

func (l *LeakyBucket) Pass() bool

func (*LeakyBucket) Set

func (l *LeakyBucket) Set(r, c float64)

type LevelDB

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

func CreateLevelDB

func CreateLevelDB(path string) (*LevelDB, error)

func (*LevelDB) CountAll

func (db *LevelDB) CountAll() (int64, error)

func (*LevelDB) CountPrefixSubsetKey

func (db *LevelDB) CountPrefixSubsetKey(key string) (int64, error)

CountPrefixSubsetKey 计算指定前缀 key 的数量

func (*LevelDB) Delete

func (db *LevelDB) Delete(key string) error

func (*LevelDB) DeleteAll

func (db *LevelDB) DeleteAll() (bool, error)

func (*LevelDB) DeletePrefixSubsetKey

func (db *LevelDB) DeletePrefixSubsetKey(key string) (bool, error)

func (*LevelDB) Get

func (db *LevelDB) Get(key string) ([]byte, error)

func (*LevelDB) Has

func (db *LevelDB) Has(key string) (bool, error)

func (*LevelDB) Put

func (db *LevelDB) Put(key string, value interface{}) error

func (*LevelDB) SelectAll

func (db *LevelDB) SelectAll() iterator.Iterator

func (*LevelDB) SelectPrefixSubsetKeyAll

func (db *LevelDB) SelectPrefixSubsetKeyAll(key string) ([]map[string]interface{}, error)

SelectPrefixSubsetKeyAll 取出所有指定前缀 key 的数据

type MongoDb

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

func NewMongoDb

func NewMongoDb(c *mongo.Client, addr string) (*MongoDb, error)

NewMongoDb mongodb 连接

func (*MongoDb) BatchInsertCollection

func (m *MongoDb) BatchInsertCollection(c *mongo.Client, db string, collection string, data []interface{}) (
	res *mongo.InsertManyResult, err error)

BatchInsertCollection 批量写入

func (*MongoDb) CountCollection

func (m *MongoDb) CountCollection(c *mongo.Client, db string, collection string, filter map[string]interface{}) (int64, int64, error)

CountCollection 统计

func (*MongoDb) DeleteOneRecord

func (m *MongoDb) DeleteOneRecord(c *mongo.Client, db string, collection string, filter map[string]interface{}) (
	res *mongo.DeleteResult, err error)

DeleteOneRecord 删除单条记录

func (*MongoDb) InsertCollection

func (m *MongoDb) InsertCollection(c *mongo.Client, db string, collection string, data interface{}) (
	res *mongo.InsertOneResult, err error)

InsertCollection 写入 db 写入的数据库 collection 写入的文档(表) data 写入的数据

func (*MongoDb) UpdateOneRecord

func (m *MongoDb) UpdateOneRecord(c *mongo.Client, db string, collection string, id string, data bson.D) (
	res *mongo.UpdateResult, err error)

UpdateOneRecord 修改单条记录

type TokenBucket

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

func (*TokenBucket) Pass

func (l *TokenBucket) Pass() bool

func (*TokenBucket) Set

func (l *TokenBucket) Set(r, c int64)

type ToolsSlice

type ToolsSlice struct{}

func (*ToolsSlice) CompareSliceByte

func (w *ToolsSlice) CompareSliceByte(s1, s2 []byte) bool

func (*ToolsSlice) CompareSliceInt

func (w *ToolsSlice) CompareSliceInt(s1, s2 []int) bool

func (*ToolsSlice) CompareSliceStr

func (w *ToolsSlice) CompareSliceStr(s1, s2 []string) bool

func (*ToolsSlice) DifferentSet

func (w *ToolsSlice) DifferentSet(s1, s2 []string) []string

DifferentSet 取两个列表的差集

func (*ToolsSlice) ExpandStringSlice

func (w *ToolsSlice) ExpandStringSlice(s []string, split string) []string

func (*ToolsSlice) InSlice

func (w *ToolsSlice) InSlice(s string, ts []string) bool

InSlice 检查字符串是否存在字符串切片中

func (*ToolsSlice) InSliceInterface

func (w *ToolsSlice) InSliceInterface(s interface{}, ts []interface{}) bool

InSliceInterface 检查Interface是否存在Interface切片中

func (*ToolsSlice) Intersect

func (w *ToolsSlice) Intersect(s1, s2 []string) []string

Intersect 取两个列表的交集

func (*ToolsSlice) Merge

func (w *ToolsSlice) Merge(s1, s2 []string) []string

Merge 合集

func (*ToolsSlice) MergeRepeatedElement

func (w *ToolsSlice) MergeRepeatedElement(s1, s2 []string) []string

MergeRepeatedElement 合集去重

func (*ToolsSlice) MergeSlice

func (w *ToolsSlice) MergeSlice(s1, s2 []interface{}) (s3 []interface{})

MergeSlice merge two interface slices to one slice

func (*ToolsSlice) MoveStr2Slice

func (w *ToolsSlice) MoveStr2Slice(str string, buf []byte)

func (*ToolsSlice) RandSlice

func (w *ToolsSlice) RandSlice(min, max int) []int

func (*ToolsSlice) RemoveRepeatedElement

func (w *ToolsSlice) RemoveRepeatedElement(arr []string) (newArr []string)

RemoveRepeatedElement 数组切片去重

func (*ToolsSlice) SliceChunk

func (w *ToolsSlice) SliceChunk(s []interface{}, size int) (chunkSlice [][]interface{})

func (*ToolsSlice) SliceDiff

func (w *ToolsSlice) SliceDiff(s1, s2 []interface{}) (s3 []interface{})

func (*ToolsSlice) SliceFilter

func (w *ToolsSlice) SliceFilter(s []interface{}, a filterType) (ftSlice []interface{})

func (*ToolsSlice) SliceHex2SliceUInt16

func (w *ToolsSlice) SliceHex2SliceUInt16(s []string) (ret []uint16)

func (*ToolsSlice) SliceIntersect

func (w *ToolsSlice) SliceIntersect(s1, s2 []interface{}) (s3 []interface{})

func (*ToolsSlice) SlicePad

func (w *ToolsSlice) SlicePad(slice []interface{}, size int, val interface{}) []interface{}

func (*ToolsSlice) SliceRand

func (w *ToolsSlice) SliceRand(a []interface{}) (b interface{})

func (*ToolsSlice) SliceRange

func (w *ToolsSlice) SliceRange(start, end, step int64) (intSlice []int64)

func (*ToolsSlice) SliceReduce

func (w *ToolsSlice) SliceReduce(s []interface{}, a reduceType) (s2 []interface{})

func (*ToolsSlice) SliceShuffle

func (w *ToolsSlice) SliceShuffle(slice []interface{}) []interface{}

func (*ToolsSlice) SliceStr2SliceInt

func (w *ToolsSlice) SliceStr2SliceInt(s []string) (ret []int)

func (*ToolsSlice) SliceSum

func (w *ToolsSlice) SliceSum(s []int64) (sum int64)

func (*ToolsSlice) SliceUnique

func (w *ToolsSlice) SliceUnique(slice []interface{}) (uniqueSlice []interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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