Documentation ¶
Index ¶
- Constants
- Variables
- func AddSlashes(s string) string
- func CmdExamples(ex ...string) string
- func ContentType(r *http.Request) string
- func CreateFile(filename string) (*os.File, error)
- func DebugLogger(options ...zap.Option) *zap.Logger
- func ExcelColumnIndex(name string) int
- func IP2Long(ip string) uint32
- func ImageCrop(w io.Writer, filename string, rect *Rect, options ...imaging.EncodeOption) error
- func ImageCropFromReader(w io.Writer, r io.Reader, format imaging.Format, rect *Rect, ...) error
- func ImageLabel(w io.Writer, filename string, rects []*Rect, options ...imaging.EncodeOption) error
- func ImageLabelFromReader(w io.Writer, r io.Reader, format imaging.Format, rects []*Rect, ...) error
- func ImageThumbnail(w io.Writer, filename string, rect *Rect, options ...imaging.EncodeOption) error
- func ImageThumbnailFromReader(w io.Writer, r io.Reader, format imaging.Format, rect *Rect, ...) error
- func IsUniqueDuplicateError(err error) bool
- func Long2IP(ip uint32) string
- func MapForm(ptr any, form map[string][]string) error
- func MapFormByTag(ptr any, form map[string][]string, tag string) error
- func MapQuery(ptr any, m map[string][]string) error
- func MappingByPtr(ptr any, setter setter, tag string) error
- func MarshalNoEscapeHTML(v any) ([]byte, error)
- func MyTimeEncoder(t time.Time, e zapcore.PrimitiveArrayEncoder)
- func NewDB(cfg *DBConfig) (*sql.DB, error)
- func NewDBx(cfg *DBConfig) (*sqlx.DB, error)
- func NewHttpClient() *http.Client
- func NewLogger(cfg *LogConfig) *zap.Logger
- func Nonce(size uint8) string
- func OpenFile(filename string) (*os.File, error)
- func QuoteMeta(s string) string
- func Retry(ctx context.Context, fn func(ctx context.Context) error, attempts int, ...) (err error)
- func SliceChunk[T ~[]E, E any](list T, size int) []T
- func SliceDiff[T ~[]E, E comparable](list1 T, list2 T) (ret1 T, ret2 T)
- func SliceIn[T ~[]E, E comparable](list T, elem E) bool
- func SliceIntersect[T ~[]E, E comparable](list1 T, list2 T) T
- func SlicePinTop[T ~[]E, E any](list T, index int)
- func SlicePinTopF[T ~[]E, E any](list T, fn func(v E) bool)
- func SliceRand[T ~[]E, E any](list T, n int) T
- func SliceUnion[T ~[]E, E comparable](lists ...T) T
- func SliceUniq[T ~[]E, E comparable](list T) T
- func SliceWithout[T ~[]E, E comparable](list T, exclude ...E) T
- func StrToTime(layout, datetime string, loc *time.Location) time.Time
- func StripSlashes(s string) string
- func TimeToStr(layout string, timestamp int64, loc *time.Location) string
- func Transaction(ctx context.Context, db *sqlx.DB, ...) (err error)
- func VersionCompare(rangeVer, curVer string) (bool, error)
- func WeekAround(layout string, now time.Time) (monday, sunday string)
- type DBConfig
- type DistributedMutex
- type ILevelNode
- type ImageEXIF
- type LevelTree
- type LogConfig
- type Orientation
- type Quantity
- type Rect
- type SQLBuilder
- type SQLClause
- type SQLOption
- func CrossJoin(table string) SQLOption
- func Distinct(columns ...string) SQLOption
- func FullJoin(table, on string) SQLOption
- func GroupBy(columns ...string) SQLOption
- func Having(query string, binds ...any) SQLOption
- func Join(table, on string) SQLOption
- func LeftJoin(table, on string) SQLOption
- func Limit(n int) SQLOption
- func Offset(n int) SQLOption
- func OrderBy(columns ...string) SQLOption
- func Returning(columns ...string) SQLOption
- func RightJoin(table, on string) SQLOption
- func Select(columns ...string) SQLOption
- func Table(name string) SQLOption
- func Union(wrappers ...SQLWrapper) SQLOption
- func UnionAll(wrappers ...SQLWrapper) SQLOption
- func Where(query string, binds ...any) SQLOption
- func WhereIn(query string, binds ...any) SQLOption
- type SQLWrapper
- type Step
- type TXBuilder
- type X
Constants ¶
const ( HeaderAccept = "Accept" HeaderAuthorization = "Authorization" HeaderContentType = "Content-Type" )
const ( ContentText = "text/plain; charset=utf-8" ContentJSON = "application/json" ContentForm = "application/x-www-form-urlencoded" ContentStream = "application/octet-stream" ContentFormMultipart = "multipart/form-data" )
const ( // B - Byte size B Quantity = 1 // KiB - KibiByte size KiB = 1024 * B // MiB - MebiByte size MiB = 1024 * KiB // GiB - GibiByte size GiB = 1024 * MiB // TiB - TebiByte size TiB = 1024 * GiB )
const MaxFormMemory = 32 << 20
const MediaThumbnailWidth = 200
Variables ¶
var ( // ErrSQLDataType 不合法的插入或更新数据类型错误 ErrSQLDataType = errors.New("invaild data type, expects: struct, *struct, yiigo.X") // ErrSQLBatchDataType 不合法的批量插入数据类型错误 ErrSQLBatchDataType = errors.New("invaild data type, expects: []struct, []*struct, []yiigo.X") )
var GMT8 = time.FixedZone("CST", 8*3600)
GMT8 东八区时区
var RestyClient = resty.NewWithClient(NewHttpClient())
RestyClient default client for http request
Functions ¶
func CmdExamples ¶ added in v1.13.2
CmdExamples formats the given examples to the cli.
func ContentType ¶ added in v1.6.5
func CreateFile ¶ added in v1.6.4
CreateFile 创建或清空指定的文件 文件已存在,则清空;文件或目录不存在,则以0775权限创建
func ExcelColumnIndex ¶ added in v1.13.2
ExcelColumnIndex 返回Excel列名对应的序号,如:A=0,B=1,AA=26,AB=27
func ImageCropFromReader ¶ added in v1.13.2
func ImageCropFromReader(w io.Writer, r io.Reader, format imaging.Format, rect *Rect, options ...imaging.EncodeOption) error
ImageCropFromReader 图片裁切
func ImageLabel ¶ added in v1.13.2
ImageLabel 图片标注
func ImageLabelFromReader ¶ added in v1.13.2
func ImageLabelFromReader(w io.Writer, r io.Reader, format imaging.Format, rects []*Rect, options ...imaging.EncodeOption) error
ImageLabelFromReader 图片标注
func ImageThumbnail ¶ added in v1.13.2
func ImageThumbnail(w io.Writer, filename string, rect *Rect, options ...imaging.EncodeOption) error
ImageThumbnail 图片缩略图
func ImageThumbnailFromReader ¶ added in v1.13.2
func ImageThumbnailFromReader(w io.Writer, r io.Reader, format imaging.Format, rect *Rect, options ...imaging.EncodeOption) error
ImageThumbnailFromReader 图片缩略图
func IsUniqueDuplicateError ¶ added in v1.12.4
IsUniqueDuplicateError 判断是否「唯一索引冲突」错误
func MapFormByTag ¶ added in v1.6.5
func MappingByPtr ¶ added in v1.6.5
func MarshalNoEscapeHTML ¶ added in v1.7.5
MarshalNoEscapeHTML 不带HTML转义的JSON序列化
func MyTimeEncoder ¶
func MyTimeEncoder(t time.Time, e zapcore.PrimitiveArrayEncoder)
MyTimeEncoder 自定义时间格式化
func NewHttpClient ¶ added in v1.14.0
NewHttpClient returns a http client
func Retry ¶ added in v1.12.4
func Retry(ctx context.Context, fn func(ctx context.Context) error, attempts int, sleep time.Duration) (err error)
Retry 重试
func SliceChunk ¶ added in v1.13.1
SliceChunk 集合分片
func SliceDiff ¶ added in v1.12.5
func SliceDiff[T ~[]E, E comparable](list1 T, list2 T) (ret1 T, ret2 T)
SliceDiff 返回两个集合之间的差异
func SliceIn ¶ added in v1.12.5
func SliceIn[T ~[]E, E comparable](list T, elem E) bool
SliceIn 返回指定元素是否在集合中
func SliceIntersect ¶ added in v1.12.5
func SliceIntersect[T ~[]E, E comparable](list1 T, list2 T) T
SliceIntersect 返回两个集合的交集
func SlicePinTop ¶ added in v1.13.1
SlicePinTop 置顶集合中的一个元素
func SlicePinTopF ¶ added in v1.13.1
SlicePinTopF 置顶集合中满足条件的一个元素
func SliceUnion ¶ added in v1.12.5
func SliceUnion[T ~[]E, E comparable](lists ...T) T
SliceUnion 返回两个集合的并集
func SliceWithout ¶ added in v1.12.5
func SliceWithout[T ~[]E, E comparable](list T, exclude ...E) T
SliceWithout 返回不包括所有给定值的切片
func Transaction ¶ added in v1.9.0
func Transaction(ctx context.Context, db *sqlx.DB, fn func(ctx context.Context, tx *sqlx.Tx) error) (err error)
Transaction 执行数据库事物
func VersionCompare ¶
VersionCompare 语义化的版本比较,支持:>, >=, =, !=, <, <=, | (or), & (and). 参数 `rangeVer` 示例:1.0.0, =1.0.0, >2.0.0, >=1.0.0&<2.0.0, <2.0.0|>3.0.0, !=4.0.4
Types ¶
type DBConfig ¶ added in v1.6.0
type DBConfig struct { // Driver 驱动名称 Driver string // DSN 数据源名称 // - [-- MySQL] username:password@tcp(localhost:3306)/dbname?timeout=10s&charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=True&loc=Local // - [Postgres] host=localhost port=5432 user=root password=secret dbname=test search_path=schema connect_timeout=10 sslmode=disable // - [- SQLite] file::memory:?cache=shared DSN string // MaxOpenConns 设置最大可打开的连接数 MaxOpenConns int // MaxIdleConns 连接池最大闲置连接数 MaxIdleConns int // ConnMaxLifetime 连接的最大生命时长 ConnMaxLifetime time.Duration // ConnMaxIdleTime 连接最大闲置时间 ConnMaxIdleTime time.Duration }
DBConfig 数据库初始化配置
type DistributedMutex ¶ added in v1.6.5
type DistributedMutex interface { // Lock 获取锁 Lock(ctx context.Context) (bool, error) // TryLock 尝试获取锁 TryLock(ctx context.Context, attempts int, delay time.Duration) (bool, error) // UnLock 释放锁 UnLock(ctx context.Context) error }
DistributedMutex 分布式锁
func RedisMutex ¶ added in v1.12.2
func RedisMutex(cli *redis.Client, key string, ttl time.Duration) DistributedMutex
RedisMutex 基于Redis实现的分布式锁实例
type ILevelNode ¶ added in v1.13.1
ILevelNode 层级树泛型约束
type ImageEXIF ¶ added in v1.13.2
type ImageEXIF struct { Size int64 Format string Width int Height int Orientation string Longitude decimal.Decimal Latitude decimal.Decimal }
ImageEXIF 定义图片EXIF
func ParseImageEXIF ¶ added in v1.13.2
ParseImageEXIF 解析图片EXIF
type LevelTree ¶ added in v1.13.1
type LevelTree[T ILevelNode] struct { Data T Children []*LevelTree[T] }
LevelTree 菜单或分类层级树
func BuildLevelTree ¶ added in v1.13.1
func BuildLevelTree[T ILevelNode](data map[int64][]T, pid int64) []*LevelTree[T]
BuildLevelTree 构建菜单或分类层级树(data=按pid归类后的数据, pid=树的起始ID)
type LogConfig ¶ added in v1.11.2
type LogConfig struct { // Filename 日志名称 Filename string // Level 日志级别 Level zapcore.Level // MaxSize 当前文件多大时轮替;默认:100MB MaxSize int // MaxAge 轮替的旧文件最大保留时长;默认:不限 MaxAge int // MaxBackups 轮替的旧文件最大保留数量;默认:不限 MaxBackups int // Compress 轮替的旧文件是否压缩;默认:不压缩 Compress bool // Stderr 是否输出到控制台 Stderr bool // Options Zap日志选项 Options []zap.Option }
LogConfig 日志初始化配置
type Orientation ¶ added in v1.13.2
type Orientation int
Orientation 图片的旋转方向
const ( TopLeft Orientation = 1 TopRight Orientation = 2 BottomRight Orientation = 3 BottomLeft Orientation = 4 LeftTop Orientation = 5 RightTop Orientation = 6 RightBottom Orientation = 7 LeftBottom Orientation = 8 )
func (Orientation) String ¶ added in v1.13.2
func (o Orientation) String() string
type SQLBuilder ¶ added in v1.0.4
type SQLBuilder interface { TXBuilder // Transaction 启用事务 Transaction(ctx context.Context, f func(ctx context.Context, tx TXBuilder) error) error }
SQLBuilder SQL构造器
func NewSQLBuilder ¶ added in v1.0.4
func NewSQLBuilder(db *sqlx.DB, logFn func(ctx context.Context, query string, args ...any)) SQLBuilder
NewSQLBuilder 生成SQL构造器
type SQLClause ¶ added in v1.0.4
type SQLClause struct {
// contains filtered or unexported fields
}
SQLClause SQL语句
type SQLOption ¶ added in v1.12.0
type SQLOption func(w *sqlWrapper)
SQLOption SQL查询选项
func Returning ¶ added in v1.12.1
Returning 指定 `RETURNING` 子句; 用于 PostgresSQL 和 SQLite(3.35.0) `INSERT` 语句
func UnionAll ¶ added in v1.1.2
func UnionAll(wrappers ...SQLWrapper) SQLOption
UnionAll 指定 `UNION ALL` 子句
type SQLWrapper ¶ added in v1.1.2
type SQLWrapper interface { // One 查询一条数据 One(ctx context.Context, data any) error // All 查询多条数据 All(ctx context.Context, data any) error // Insert 插入数据 (数据类型:`struct`, `*struct`, `yiigo.X`) Insert(ctx context.Context, data any) (sql.Result, error) // BatchInsert 批量插入数据 (数据类型:`[]struct`, `[]*struct`, `[]yiigo.X`) BatchInsert(ctx context.Context, data any) (sql.Result, error) // Update 更新数据 (数据类型:`struct`, `*struct`, `yiigo.X`) Update(ctx context.Context, data any) (sql.Result, error) // Delete 删除数据 Delete(ctx context.Context) (sql.Result, error) // Truncate 清空表 Truncate(ctx context.Context) (sql.Result, error) }
SQLWrapper SQL包装器
type TXBuilder ¶ added in v1.12.0
type TXBuilder interface { // Wrap 包装查询选项 Wrap(opts ...SQLOption) SQLWrapper // contains filtered or unexported methods }
TXBuilder 事务构造器