util

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MulanPSL-2.0 Imports: 51 Imported by: 0

README

说明

作者很懒,什么也没做说明

build

go build -ldflags "-s -w" -o [可执行文件路径] [main.go]

usage

go get -u gitee.com/binny_w/go-util

import "gitee.com/binny_w/go-util"

Documentation

Index

Constants

View Source
const (
	DateTimeAsSN   = "20060102150405"
	DateTimeFormat = "2006-01-02 15:04:05"
	DateFormat     = "2006-01-02"
	TimeFormat     = "15:04:05"
)
View Source
const AlphabetAndNumbers = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Variables

View Source
var ErrAesCiphertextTooShort = errors.New(`ciphertext is too short`)
View Source
var ErrAesCiphertextWrong = errors.New(`ciphertext is not a multiple of the block size`)
View Source
var ErrAmqpChannelClosed = errors.New(`channel is closed`)
View Source
var ErrMemCacheKeyExists = errors.New(`key已存在`)
View Source
var ErrMemCacheKeyExpired = errors.New(`key已过期`)
View Source
var ErrMemCacheKeyNotExist = errors.New(`key不存在`)

Functions

func AddSlashes

func AddSlashes(str string) string

AddSlashes 返回在预定义字符之前添加反斜杠的字符串。 预定义字符是:单引号(')、双引号(")、反斜杠(\)

func AesCBCDecrypt

func AesCBCDecrypt(str, key string) (string, error)

AesCBCDecrypt 解密

func AesCBCEncrypt

func AesCBCEncrypt(str, key string) (string, error)

AesCBCEncrypt 加密,key 16位

func FileAppend

func FileAppend(file, content string) (bool, error)

FileAppend 追加写文件

func FileDel

func FileDel(file string) (bool, error)

FileDel 删除文件或空目录

func FileDelAll

func FileDelAll(dir string) (bool, error)

FileDelAll 删除目录及包含的所有文件和子目录

func FileExist

func FileExist(file string) bool

FileExist 文件 或 目录是否存在

func FileInfo

func FileInfo(file string) (fs.FileInfo, error)

FileInfo 获得文件信息

func FileIsDir

func FileIsDir(file string) (bool, error)

FileIsDir 路径是否目录

func FileMd5

func FileMd5(file string) (string, error)

FileMd5 取文件 MD5值

func FileMkDir

func FileMkDir(dir string) (bool, error)

FileMkDir 递归创建目录

func FileRead

func FileRead(file string) ([]byte, error)

FileRead 读取文件内容

func FileReadDir

func FileReadDir(dir string) ([]string, error)

FileReadDir 读取目录下所有文件

func FileReadDirAll

func FileReadDirAll(dir string) ([]string, error)

FileReadDirAll 递归读取目录下所有文件

func FileReadLines

func FileReadLines(file string) (lines []string, err error)

FileReadLines 逐行读取文件

func FileReadPart

func FileReadPart(file string, offset, length int64) ([]byte, error)

FileReadPart 读取文件的部分内容

func FileWalkLines

func FileWalkLines(file string, fn func(l string) error) (err error)

FileWalkLines 逐行分析文件

func FileWrite

func FileWrite(file, content string) (bool, error)

FileWrite 覆盖写文件

func GetBinPath

func GetBinPath() (string, error)

GetBinPath 取可执行程序的绝对路径

func GetHomeDir

func GetHomeDir() (string, error)

GetHomeDir 获取当前用户的Home根目录路径

func HttpSrvRun added in v0.0.3

func HttpSrvRun(addr string, hdl http.Handler, onClose func())

HttpSrvRun 使用 http.Server 内置的 Shutdown() 方法优雅地关机

func InArray

func InArray(needle interface{}, haystack interface{}) (isIn bool, err error)

InArray needle 是否存在于 haystack 中

func InterfaceToFloat64

func InterfaceToFloat64(v interface{}) (ret float64)

InterfaceToFloat64 interface 转为 float64

func InterfaceToInt

func InterfaceToInt(v interface{}) (ret int)

InterfaceToInt interface 转为 int

func InterfaceToInt64

func InterfaceToInt64(v interface{}) (ret int64)

InterfaceToInt64 interface 转为 int64

func InterfaceToStr

func InterfaceToStr(v interface{}) (ret string)

InterfaceToStr interface 转为 string

func LocalIp

func LocalIp() string

LocalIp 获取内网 IP

func MongoClient

func MongoClient(dsn string) (*mongo.Client, error)

MongoClient Mongo连接客户端

func MongoPing

func MongoPing(client *mongo.Client) error

MongoPing 验证连通性

func PKCS7Padding

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

PKCS7Padding 补位

func PKCS7UnPadding

func PKCS7UnPadding(origData []byte) []byte

PKCS7UnPadding 取消补位

func RandInt

func RandInt(i int) int

RandInt 获得随机数

func RandRange added in v0.0.10

func RandRange(min, max int) int

RandRange 按范围取随机值 [min, max)

func RedisDel

func RedisDel(rc redis.Conn, key ...interface{}) (int64, error)

RedisDel Redis 删除一个或多个 Key

func RedisExpire

func RedisExpire(rc redis.Conn, key string, period time.Duration) (bool, error)

RedisExpire Redis 设置过期时间

func RedisIncr

func RedisIncr(rc redis.Conn, key string, step int) (int64, error)

RedisIncr Redis 计数器

func RedisPool

func RedisPool(name string, options ...redis.DialOption) (*redis.Pool, error)

RedisPool 获取一个连接池 参考文档: https://pkg.go.dev/github.com/gomodule/redigo/redis Redis 命令全集: http://doc.redisfans.com/index.html

func RedisRateLimit

func RedisRateLimit(rc redis.Conn, key string, rate uint, period time.Duration) (bool, error)

RedisRateLimit Redis的令牌桶方式限流

func RedisSet

func RedisSet(rc redis.Conn, key string, value string, period time.Duration, NXorXX string) (bool, error)

RedisSet 写数据,支持 px/nx/xx

func Retry

func Retry(attempt uint, sleep time.Duration, fn func() error) error

Retry 重试

func RsaGenKeys

func RsaGenKeys(bits int) (publicKey, privateKey []byte, err error)

RsaGenKeys 生成密钥对 bits = 1024/2048

func ShellExec

func ShellExec(cmdBin string, args ...string) (ret []string, err error)

ShellExec 执行命令

func ShellExecWalk

func ShellExecWalk(fn func(s string) error, cmdBin string, args ...string) error

ShellExecWalk 逐行处理命令执行结果

func StrAToFloat64

func StrAToFloat64(s string) (f float64, err error)

StrAToFloat64 字符串表示的10的-18次方,转为float64

func StrCamelToCase

func StrCamelToCase(s string) string

StrCamelToCase 驼峰转下划线

func StrCaseToCamel

func StrCaseToCamel(s string) string

StrCaseToCamel 下划线转驼峰

func StrCheckPass

func StrCheckPass(pass, hash string) bool

StrCheckPass 验证密码,是否能匹配

func StrEndWith

func StrEndWith(str, find string) bool

StrEndWith 判断字符串 str 是否以 find 结尾

func StrFileName

func StrFileName(name string) (string, error)

StrFileName 字符串能否作为文件名

func StrHashPass

func StrHashPass(pass string) (string, error)

StrHashPass 对密码做Hash

func StrHexDecode

func StrHexDecode(s string) ([]byte, error)

StrHexDecode 将16进制字符串,转为字节切片

func StrHexEncode

func StrHexEncode(bs []byte) string

StrHexEncode 将16进制字节切片,转为字符串

func StrImplode

func StrImplode(gap string, ss ...string) string

StrImplode 多个字符串拼接

func StrMd5

func StrMd5(s string) string

StrMd5 计算字符串 MD5 值

func StrNiceSize

func StrNiceSize(sizeI int64) string

StrNiceSize 友好方式显示文件大小

func StrRandom

func StrRandom(l int, c string) string

StrRandom 得到指定长度的随机字符串

func StrSha1

func StrSha1(s string) string

StrSha1 计算字符串 Sha1 值

func StrSha256

func StrSha256(s string) string

StrSha256 sha256加密

func StrStartWith

func StrStartWith(str, find string) bool

StrStartWith 判断字符串 str 是否以 find 开始

func StrSubZh

func StrSubZh(str string, start, end uint) string

StrSubZh 中文字符串截取

func StrTrim

func StrTrim(s string) string

StrTrim 去除字符串左右空白

func StripSlashes

func StripSlashes(str string) string

StripSlashes 删除由 AddSlashes() 函数添加的反斜杠。

func StripXSS

func StripXSS(s string, isUGC bool) string

StripXSS 去除XSS内容

func TimeDateFromStamp

func TimeDateFromStamp(ts int64) string

TimeDateFromStamp 时间戳转日期

func TimeDateToStamp

func TimeDateToStamp(str string) (int64, error)

TimeDateToStamp 日期转时间戳

func TimeFormStamp

func TimeFormStamp(ts int64) string

TimeFormStamp 时间戳转时间

func TimeLocationBJ

func TimeLocationBJ() *time.Location

TimeLocationBJ 北京、上海、重庆时区

func TimeNice

func TimeNice(t time.Time) string

TimeNice 按常用格式输出日期时间

func TimeNowAsSN

func TimeNowAsSN() string

TimeNowAsSN 输出当前日期时间

func TimeNowDateNice

func TimeNowDateNice() string

TimeNowDateNice 输出当前日期

func TimeNowNice

func TimeNowNice() string

TimeNowNice 输出当前日期时间

func TimeNowTimeNice

func TimeNowTimeNice() string

TimeNowTimeNice 输出当前时间

func TimeToStamp

func TimeToStamp(str string) (int64, error)

TimeToStamp 时间转时间戳

func VarDump

func VarDump(v ...interface{})

VarDump 打印变量

Types

type AmqpClient

type AmqpClient struct {
	Connection *amqp.Connection
	// contains filtered or unexported fields
}

func NewAmqpClient

func NewAmqpClient(url string) (*AmqpClient, error)

NewAmqpClient 新的连接池

func (*AmqpClient) Close

func (ac *AmqpClient) Close() error

Close 关闭连接

func (*AmqpClient) GetChannel

func (ac *AmqpClient) GetChannel() (*amqp.Channel, error)

GetChannel 获取一个连接

func (*AmqpClient) PutChannel

func (ac *AmqpClient) PutChannel(ch *amqp.Channel)

PutChannel 归还一个连接

type MemCache

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

func NewMemCache

func NewMemCache() *MemCache

NewMemCache 得到一个缓存

func (*MemCache) Clear

func (s *MemCache) Clear()

Clear 清空

func (*MemCache) Del

func (s *MemCache) Del(k string)

Del 删除

func (*MemCache) ExpireAt added in v0.0.5

func (s *MemCache) ExpireAt(k string) int64

ExpireAt 查询有效期,为0时表示永不过期

func (*MemCache) GC added in v0.0.6

func (s *MemCache) GC(td time.Duration, num uint64)

GC 过期Key回收的时间间隔和每次的数量

func (*MemCache) Get

func (s *MemCache) Get(k string) (any, error)

Get 获取

func (*MemCache) Set

func (s *MemCache) Set(k string, v any, exp int64)

Set 设置

func (*MemCache) SetExpire added in v0.0.5

func (s *MemCache) SetExpire(k string, exp int64) error

SetExpire 设置过期时间

func (*MemCache) SetNX

func (s *MemCache) SetNX(k string, v any, exp int64) error

SetNX set nx

type MongoCollection

type MongoCollection struct {
	Collection *mongo.Collection
	Logger     func(ctx context.Context, log *MongoOperationLog)
}

func NewMongoCollection

func NewMongoCollection(cli *mongo.Client, db, col string, fn func(ctx context.Context, log *MongoOperationLog)) *MongoCollection

NewMongoCollection 新连接

func (*MongoCollection) BulkWrite

func (mc *MongoCollection) BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (res *mongo.BulkWriteResult, err error)

BulkWrite 批量写入指令

func (*MongoCollection) CountDocuments

func (mc *MongoCollection) CountDocuments(ctx context.Context, where any, opts ...*options.CountOptions) (int64, error)

CountDocuments 统计记录数量

func (*MongoCollection) CursorAll

func (mc *MongoCollection) CursorAll(ctx context.Context, cursor *mongo.Cursor) ([]bson.M, error)

CursorAll 一次性拿到全部结果

func (*MongoCollection) CursorWalk

func (mc *MongoCollection) CursorWalk(ctx context.Context, fn func(v bson.M) error, cursor *mongo.Cursor) error

CursorWalk 逐条处理结果数据

func (*MongoCollection) DeleteMany

func (mc *MongoCollection) DeleteMany(ctx context.Context, where any, opts ...*options.DeleteOptions) (num int64, err error)

DeleteMany 删除多条数据

func (*MongoCollection) DeleteOne

func (mc *MongoCollection) DeleteOne(ctx context.Context, where any, opts ...*options.DeleteOptions) (num int64, err error)

DeleteOne 删除一条记录

func (*MongoCollection) Distinct

func (mc *MongoCollection) Distinct(ctx context.Context, name string, where any, opts ...*options.DistinctOptions) ([]any, error)

Distinct 查找并去重复

func (*MongoCollection) Drop

func (mc *MongoCollection) Drop(ctx context.Context) (err error)

Drop 清空

func (*MongoCollection) Find

func (mc *MongoCollection) Find(ctx context.Context, where any, opts ...*options.FindOptions) ([]bson.M, error)

Find 按条件查找记录

func (*MongoCollection) FindOne

func (mc *MongoCollection) FindOne(ctx context.Context, where any, opts ...*options.FindOneOptions) (bson.M, error)

FindOne 查找一条记录

func (*MongoCollection) FindOneAndDelete

func (mc *MongoCollection) FindOneAndDelete(ctx context.Context, where any, opts ...*options.FindOneAndDeleteOptions) (res bson.M, err error)

FindOneAndDelete 找到一条记录并删除

func (*MongoCollection) FindOneAndReplace

func (mc *MongoCollection) FindOneAndReplace(ctx context.Context, where, set any, opts ...*options.FindOneAndReplaceOptions) (res bson.M, err error)

FindOneAndReplace 找到一条并替换

func (*MongoCollection) FindOneAndUpdate

func (mc *MongoCollection) FindOneAndUpdate(ctx context.Context, where, set any, opts ...*options.FindOneAndUpdateOptions) (res bson.M, err error)

FindOneAndUpdate 找到一条并更新

func (*MongoCollection) FindWalk

func (mc *MongoCollection) FindWalk(ctx context.Context, fn func(v bson.M) error, where any, opts ...*options.FindOptions) error

FindWalk 按条件查找并逐条处理

func (*MongoCollection) IndexesCreateMany

func (mc *MongoCollection) IndexesCreateMany(ctx context.Context, models []mongo.IndexModel, opts ...*options.CreateIndexesOptions) (res []string, err error)

IndexesCreateMany 创建多条索引

func (*MongoCollection) IndexesCreateOne

func (mc *MongoCollection) IndexesCreateOne(ctx context.Context, model mongo.IndexModel, opts ...*options.CreateIndexesOptions) (res string, err error)

IndexesCreateOne 创建一条索引

func (*MongoCollection) IndexesDropAll

func (mc *MongoCollection) IndexesDropAll(ctx context.Context, opts ...*options.DropIndexesOptions) (res bson.Raw, err error)

IndexesDropAll 删除全部索引

func (*MongoCollection) IndexesDropOne

func (mc *MongoCollection) IndexesDropOne(ctx context.Context, name string, opts ...*options.DropIndexesOptions) (res bson.Raw, err error)

IndexesDropOne 删除一条索引

func (*MongoCollection) IndexesList

func (mc *MongoCollection) IndexesList(ctx context.Context, opts ...*options.ListIndexesOptions) ([]bson.M, error)

IndexesList 获取索引

func (*MongoCollection) InsertMany

func (mc *MongoCollection) InsertMany(ctx context.Context, items []any, opts ...*options.InsertManyOptions) (newIds []any, err error)

InsertMany 插入多条数据

func (*MongoCollection) InsertOne

func (mc *MongoCollection) InsertOne(ctx context.Context, item any, opts ...*options.InsertOneOptions) (newId any, err error)

InsertOne 插入一条数据

func (*MongoCollection) Names

func (mc *MongoCollection) Names() (string, string)

Names 获取库名和表名

func (*MongoCollection) NewOperationLog

func (mc *MongoCollection) NewOperationLog(do string, set, whr, opt, res any, err error) *MongoOperationLog

NewOperationLog 一条日志

func (*MongoCollection) PageWalk

func (mc *MongoCollection) PageWalk(
	ctx context.Context,
	fn func(v bson.M) error,
	page, pageSize int64,
	where, sort any,
	opts ...*options.FindOptions,
) (itemTotal, pageTotal int64, err error)

PageWalk 分页取数据

func (*MongoCollection) UpdateByID

func (mc *MongoCollection) UpdateByID(ctx context.Context, id, set any, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)

UpdateByID 按ID更新数据

func (*MongoCollection) UpdateMany

func (mc *MongoCollection) UpdateMany(ctx context.Context, where, set any, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)

UpdateMany 按条件更新多条数据

func (*MongoCollection) UpdateOne

func (mc *MongoCollection) UpdateOne(ctx context.Context, where, set any, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)

UpdateOne 按条件更新一条数据

type MongoOperationLog

type MongoOperationLog bson.M

type Mysql

type Mysql struct {
	ConnParam     *MysqlConnParam
	MasterAsSlave bool
	TimeoutInsert time.Duration
	TimeoutSelect time.Duration
	TimeoutUpdate time.Duration
	TimeoutTrans  time.Duration
	Logger        *Queue
	Listener      func(err error, query string, param ...interface{})
}

Mysql 连接实例

func NewMysql

func NewMysql(param *MysqlConnParam) *Mysql

NewMysql 得到一个连接实例

func (*Mysql) AddLog

func (m *Mysql) AddLog(query string, params []interface{}, err error)

AddLog 添加日志

func (*Mysql) AllColumns added in v0.0.11

func (m *Mysql) AllColumns(table string) ([]string, error)

AllColumns 表所有的字段

func (*Mysql) CloseDB

func (m *Mysql) CloseDB()

CloseDB 关闭连接

func (*Mysql) ColumnTypes added in v0.0.11

func (m *Mysql) ColumnTypes(table string) (map[string]string, error)

ColumnTypes 字段对应类型

func (*Mysql) ColumnsWalk added in v0.0.11

func (m *Mysql) ColumnsWalk(fn func(row *MysqlRow) error, table string) error

ColumnsWalk 查询数据表结构

func (*Mysql) Count

func (m *Mysql) Count(query string, params ...interface{}) (cnt int64, err error)

Count 查询数据结果个数

func (*Mysql) DelByIds

func (m *Mysql) DelByIds(table, pk string, ids ...interface{}) (i int64, err error)

DelByIds 按一个或多个ID删除数据

func (*Mysql) Delete

func (m *Mysql) Delete(query string, params ...interface{}) (int64, error)

Delete 删除数据

func (*Mysql) GetByIds

func (m *Mysql) GetByIds(table, pk string, ids []interface{}, cols ...string) (rows []*MysqlRow, err error)

GetByIds 按多个ID取数据

func (*Mysql) GetDB

func (m *Mysql) GetDB(useSlave bool) (*sql.DB, error)

GetDB 获取一个连接

func (*Mysql) GetOne

func (m *Mysql) GetOne(table, pk, id string, cols ...string) (*MysqlRow, error)

GetOne 按ID取一条数据

func (*Mysql) IdsWalk

func (m *Mysql) IdsWalk(fn func(row *MysqlRow) error, table string, pk string, ids []interface{}, cols ...string) error

IdsWalk 按多个ID取数据并逐条处理

func (*Mysql) Insert

func (m *Mysql) Insert(table string, row *MysqlRow, ignoreOrReplace ...bool) (int64, error)

Insert 插入一条数据

func (*Mysql) InsertBatch

func (m *Mysql) InsertBatch(table string, rows []*MysqlRow) (ret int64, err error)

InsertBatch 批量插入

func (*Mysql) InsertDuplicate

func (m *Mysql) InsertDuplicate(table string, row *MysqlRow) (int64, error)

InsertDuplicate 插入,如果存在就更新

func (*Mysql) InsertIgnore

func (m *Mysql) InsertIgnore(table string, row *MysqlRow) (int64, error)

InsertIgnore 插入,如果存在就忽略

func (*Mysql) InsertReplace

func (m *Mysql) InsertReplace(table string, row *MysqlRow) (int64, error)

InsertReplace 插入,如果存在就替换

func (*Mysql) PopLogs

func (m *Mysql) PopLogs(fn func(ql *MysqlLog) error) error

PopLogs 处理日志

func (*Mysql) SelPage

func (m *Mysql) SelPage(page, numPerPage int64, table, queryWhere, queryOrder, selCols string, fn func(row *MysqlRow) error, params ...interface{}) (totalItem, totalPage, curPage int64, err error)

SelPage 按页查询

func (*Mysql) SelWalk

func (m *Mysql) SelWalk(fn func(row *MysqlRow) error, query string, params ...interface{}) error

SelWalk 查询并逐条处理

func (*Mysql) Select

func (m *Mysql) Select(query string, params ...interface{}) (rows []*MysqlRow, err error)

Select 查询数据

func (*Mysql) SetListener

func (m *Mysql) SetListener(fn func(err error, query string, param ...interface{}))

SetListener 设置监听函数

func (*Mysql) Transaction

func (m *Mysql) Transaction(fn func(tx *sql.Tx, addLog func(query string, params []interface{}, err error)) error, opts *sql.TxOptions) error

Transaction 事务

func (*Mysql) Truncate

func (m *Mysql) Truncate(table string) error

Truncate 清空一张表

func (*Mysql) Update

func (m *Mysql) Update(query string, params ...interface{}) (int64, error)

Update 更新数据

func (*Mysql) UpdateById

func (m *Mysql) UpdateById(table string, row *MysqlRow, pk, id string) (int64, error)

UpdateById 按ID更新数据

type MysqlConnOption

type MysqlConnOption struct {
	Dsn             string
	Addr            string
	DBName          string
	MaxOpenConn     int
	MaxIdleConn     int
	ConnMaxIdleTime time.Duration
	ConnMaxLifetime time.Duration
}

MysqlConnOption 单库连接配置

func NewMysqlConnOption

func NewMysqlConnOption(dsn string, maxOpen, maxIdle int, maxIdleTime, maxLifeTime time.Duration) (*MysqlConnOption, error)

NewMysqlConnOption 得到一个单库连接配置

type MysqlConnParam

type MysqlConnParam struct {
	Master *MysqlConnOption
	Slaves []*MysqlConnOption
}

MysqlConnParam 连接配置

type MysqlDBs

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

MysqlDBs 存放连接

type MysqlLog

type MysqlLog struct {
	When   string
	Query  string
	Params []interface{}
	Err    error
}

MysqlLog 操作日志

type MysqlRow

type MysqlRow map[string]any

MysqlRow 数据单元

func (*MysqlRow) ToInt64

func (row *MysqlRow) ToInt64(key string) int64

ToInt64 查询结果转整形

func (*MysqlRow) ToNum

func (row *MysqlRow) ToNum(key string) float64

ToNum 查询结果转数字

func (*MysqlRow) ToStr

func (row *MysqlRow) ToStr(key string) string

ToStr 查询结果转字符串

type MysqlTable

type MysqlTable struct {
	Conn *Mysql
	Name string
	PK   string
}

MysqlTable Mysql数据表

func NewMysqlTable

func NewMysqlTable(mysql *Mysql, tableName, primaryKey string) (*MysqlTable, error)

NewMysqlTable 实例化数据表

func (*MysqlTable) AllWalk added in v0.0.7

func (mt *MysqlTable) AllWalk(fn func(row *MysqlRow) error, cols ...string) error

AllWalk 使用函数遍历全部数据

func (*MysqlTable) ColumnTypes added in v0.0.11

func (mt *MysqlTable) ColumnTypes() (map[string]string, error)

ColumnTypes 表所有字段及对应类型

func (*MysqlTable) Count

func (mt *MysqlTable) Count(query string, params ...interface{}) (int64, error)

Count 查询数据行数

func (*MysqlTable) DelByIds

func (mt *MysqlTable) DelByIds(ids ...interface{}) (int64, error)

DelByIds 按主键删除一条或多条

func (*MysqlTable) Delete

func (mt *MysqlTable) Delete(query string, params ...interface{}) (int64, error)

Delete 删除数据

func (*MysqlTable) GetByIds

func (mt *MysqlTable) GetByIds(ids []interface{}, cols ...string) ([]*MysqlRow, error)

GetByIds 批量按主键查询

func (*MysqlTable) GetOne

func (mt *MysqlTable) GetOne(id string, cols ...string) (*MysqlRow, error)

GetOne 按主键查询一条

func (*MysqlTable) IdsWalk

func (mt *MysqlTable) IdsWalk(fn func(row *MysqlRow) error, ids []interface{}, cols ...string) error

IdsWalk 按主键查询多条,并执行指定函数

func (*MysqlTable) Insert

func (mt *MysqlTable) Insert(row *MysqlRow) (int64, error)

Insert 插入新记录

func (*MysqlTable) InsertBatch

func (mt *MysqlTable) InsertBatch(rows []*MysqlRow) (int64, error)

InsertBatch 批量插入

func (*MysqlTable) InsertDuplicate

func (mt *MysqlTable) InsertDuplicate(row *MysqlRow) (int64, error)

InsertDuplicate 唯一索引约束,不存在则新增,存在则更新

func (*MysqlTable) InsertIgnore

func (mt *MysqlTable) InsertIgnore(row *MysqlRow) (int64, error)

InsertIgnore 唯一索引约束,存在则忽略

func (*MysqlTable) InsertReplace

func (mt *MysqlTable) InsertReplace(row *MysqlRow) (int64, error)

InsertReplace 唯一索引约束,不存在则新增,存在则先删再增

func (*MysqlTable) SelPage

func (mt *MysqlTable) SelPage(fn func(row *MysqlRow) error, page, numPerPage int64, queryWhere, queryOrder, selCols string, params ...interface{}) (int64, int64, int64, error)

SelPage 按页查询

func (*MysqlTable) SelWalk

func (mt *MysqlTable) SelWalk(fn func(row *MysqlRow) error, query string, params ...interface{}) error

SelWalk 查询,每条记录执行指定函数

func (*MysqlTable) Select

func (mt *MysqlTable) Select(query string, params ...interface{}) ([]*MysqlRow, error)

Select 查询

func (*MysqlTable) Transaction

func (mt *MysqlTable) Transaction(fn func(tx *sql.Tx, addLog func(query string, params []interface{}, err error)) error, opts *sql.TxOptions) error

Transaction 事务操作

func (*MysqlTable) Truncate

func (mt *MysqlTable) Truncate() error

Truncate 清空表

func (*MysqlTable) Update

func (mt *MysqlTable) Update(query string, params ...interface{}) (int64, error)

Update 更新数据

func (*MysqlTable) UpdateById

func (mt *MysqlTable) UpdateById(row *MysqlRow, id string) (int64, error)

UpdateById 按主键更新

type QueryLog

type QueryLog struct {
	When   string
	Query  string
	Params []interface{}
	Err    error
}

QueryLog 查询日志结构体

type Queue

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

func NewQueue

func NewQueue(size uint64) *Queue

NewQueue 新建一个定长队列

func (*Queue) Pause

func (q *Queue) Pause(b bool)

Pause 暂停队列,忽略写入,能读出

func (*Queue) Pop

func (q *Queue) Pop() (interface{}, bool)

Pop 从队列中拿出一条数据,FIFO原则

func (*Queue) PopAll

func (q *Queue) PopAll() (ret []interface{})

PopAll 从队列中拿出所有的数据

func (*Queue) PopAllWalk

func (q *Queue) PopAllWalk(fn func(item interface{}))

PopAllWalk 逐个处理

func (*Queue) PopWait

func (q *Queue) PopWait(td time.Duration) (interface{}, error)

PopWait 从队列中拿出一条数据,阻塞等待

func (*Queue) Push

func (q *Queue) Push(items ...interface{})

Push 向队列中插入一条或多条数据,旧的数据单元有可能丢失!!!

func (*Queue) PushWait

func (q *Queue) PushWait(items ...interface{})

PushWait 向队列中插入一条或多条数据,空间充满时,将会阻塞等待

func (*Queue) TryPushOne

func (q *Queue) TryPushOne(item interface{}) bool

TryPushOne 尝试插入一条数据,立即返回成功或失败

type RateLimit

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

func NewRateLimit

func NewRateLimit(num uint64, td time.Duration) (rl *RateLimit)

NewRateLimit 获得一个实例,每 sec 秒 可以处理 num 次

func (*RateLimit) Check

func (rl *RateLimit) Check() bool

Check 是否可用,非阻塞

func (*RateLimit) CheckWait

func (rl *RateLimit) CheckWait(td time.Duration) bool

CheckWait 等待可用,阻塞,可设置超时

func (*RateLimit) Close

func (rl *RateLimit) Close()

Close 关闭

func (*RateLimit) Open

func (rl *RateLimit) Open()

Open 启动

func (*RateLimit) SetDuration added in v0.0.6

func (rl *RateLimit) SetDuration(td time.Duration)

SetDuration 动态改变间隔参数

func (*RateLimit) Status

func (rl *RateLimit) Status() int

Status 当前可用量

type RedisPoolMap

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

type RsaKeys

type RsaKeys struct {
	PublicKeyStr  string
	PrivateKeyStr string
	PublicKey     *rsa.PublicKey
	PrivateKey    *rsa.PrivateKey
}

func NewRsaKeys

func NewRsaKeys(publicKey, privateKey string) (*RsaKeys, error)

NewRsaKeys openssl genrsa -out rsa_private_key.pem 2048 openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

func (*RsaKeys) DecodeWithPrivateKey

func (rk *RsaKeys) DecodeWithPrivateKey(txt string) (string, error)

DecodeWithPrivateKey 使用私钥解密

func (*RsaKeys) EncodeWithPublicKey

func (rk *RsaKeys) EncodeWithPublicKey(txt string) (string, error)

EncodeWithPublicKey 使用公钥加密

func (*RsaKeys) SignWithPrivateKey

func (rk *RsaKeys) SignWithPrivateKey(txt string) (string, error)

SignWithPrivateKey 使用私钥签名

func (*RsaKeys) VerifyWithPublicKey

func (rk *RsaKeys) VerifyWithPublicKey(sign, txt string) error

VerifyWithPublicKey 使用公钥验签

type WaitGroup

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

func NewWaitGroup

func NewWaitGroup(size int) *WaitGroup

NewWaitGroup 生成

func (*WaitGroup) Add

func (p *WaitGroup) Add(delta int)

Add 添加或减少任务

func (*WaitGroup) Done

func (p *WaitGroup) Done()

Done 完成任务

func (*WaitGroup) Wait

func (p *WaitGroup) Wait()

Wait 等待任务完成

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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