Documentation
¶
Index ¶
- Constants
- Variables
- func AddSlashes(s string) string
- func BindForm(r *http.Request, obj any) error
- func BindJSON(r *http.Request, obj any) error
- func BindProto(r *http.Request, msg proto.Message) error
- func ContentType(h http.Header) 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 ILevelNode
- type ImageEXIF
- type LevelTree
- type LogConfig
- type Mutex
- 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" ContentXML = "application/xml" ContentForm = "application/x-www-form-urlencoded" ContentStream = "application/octet-stream" ContentMultipartForm = "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, map[string]any") // ErrSQLBatchDataType 不合法的批量插入数据类型错误 ErrSQLBatchDataType = errors.New("invaild data type, expects: []struct, []*struct, []map[string]any") )
var GMT8 = time.FixedZone("CST", 8*3600)
GMT8 东八区时区
var RestyClient = resty.NewWithClient(NewHttpClient())
RestyClient default client for http request
Functions ¶
func ContentType ¶
func CreateFile ¶
CreateFile 创建或清空指定的文件 文件已存在,则清空;文件或目录不存在,则以0775权限创建
func ExcelColumnIndex ¶
ExcelColumnIndex 返回Excel列名对应的序号,如:A=0,B=1,AA=26,AB=27
func ImageCropFromReader ¶
func ImageCropFromReader(w io.Writer, r io.Reader, format imaging.Format, rect *Rect, options ...imaging.EncodeOption) error
ImageCropFromReader 图片裁切
func ImageLabel ¶
ImageLabel 图片标注
func ImageLabelFromReader ¶
func ImageLabelFromReader(w io.Writer, r io.Reader, format imaging.Format, rects []*Rect, options ...imaging.EncodeOption) error
ImageLabelFromReader 图片标注
func ImageThumbnail ¶
func ImageThumbnail(w io.Writer, filename string, rect *Rect, options ...imaging.EncodeOption) error
ImageThumbnail 图片缩略图
func ImageThumbnailFromReader ¶
func ImageThumbnailFromReader(w io.Writer, r io.Reader, format imaging.Format, rect *Rect, options ...imaging.EncodeOption) error
ImageThumbnailFromReader 图片缩略图
func IsUniqueDuplicateError ¶
IsUniqueDuplicateError 判断是否「唯一索引冲突」错误
func MappingByPtr ¶
func MarshalNoEscapeHTML ¶
MarshalNoEscapeHTML 不带HTML转义的JSON序列化
func MyTimeEncoder ¶
func MyTimeEncoder(t time.Time, e zapcore.PrimitiveArrayEncoder)
MyTimeEncoder 自定义时间格式化
func Retry ¶
func Retry(ctx context.Context, fn func(ctx context.Context) error, attempts int, sleep time.Duration) (err error)
Retry 重试
func SliceDiff ¶
func SliceDiff[T ~[]E, E comparable](list1 T, list2 T) (ret1 T, ret2 T)
SliceDiff 返回两个集合之间的差异
func SliceIntersect ¶
func SliceIntersect[T ~[]E, E comparable](list1 T, list2 T) T
SliceIntersect 返回两个集合的交集
func SlicePinTopF ¶
SlicePinTopF 置顶集合中满足条件的一个元素
func SliceWithout ¶
func SliceWithout[T ~[]E, E comparable](list T, exclude ...E) T
SliceWithout 返回不包括所有给定值的切片
func Transaction ¶
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 ¶
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 ImageEXIF ¶
type ImageEXIF struct { Size int64 Format string Width int Height int Orientation string Longitude decimal.Decimal Latitude decimal.Decimal }
ImageEXIF 定义图片EXIF
func ParseImageEXIF ¶
ParseImageEXIF 解析图片EXIF
type LevelTree ¶
type LevelTree[T ILevelNode] struct { Data T Children []*LevelTree[T] }
LevelTree 菜单或分类层级树
func BuildLevelTree ¶
func BuildLevelTree[T ILevelNode](data map[int64][]T, pid int64) []*LevelTree[T]
BuildLevelTree 构建菜单或分类层级树(data=按pid归类后的数据, pid=树的起始ID)
type LogConfig ¶
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 Mutex ¶ added in v1.0.2
type Mutex 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 }
Mutex 分布式锁
type Orientation ¶
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 ¶
func (o Orientation) String() string
type SQLBuilder ¶
type SQLBuilder interface { TXBuilder // Transaction 启用事务 Transaction(ctx context.Context, f func(ctx context.Context, tx TXBuilder) error) error }
SQLBuilder SQL构造器
func NewSQLBuilder ¶
func NewSQLBuilder(db *sqlx.DB, logFn func(ctx context.Context, query string, args ...any)) SQLBuilder
NewSQLBuilder 生成SQL构造器
type SQLClause ¶
type SQLClause struct {
// contains filtered or unexported fields
}
SQLClause SQL语句
type SQLOption ¶
type SQLOption func(w *sqlWrapper)
SQLOption SQL查询选项
type SQLWrapper ¶
type SQLWrapper interface { // One 查询一条数据 One(ctx context.Context, data any) error // All 查询多条数据 All(ctx context.Context, data any) error // Insert 插入数据 (数据类型:`struct`, `*struct`, `map[string]any`) Insert(ctx context.Context, data any) (sql.Result, error) // BatchInsert 批量插入数据 (数据类型:`[]struct`, `[]*struct`, `[]map[string]any`) BatchInsert(ctx context.Context, data any) (sql.Result, error) // Update 更新数据 (数据类型:`struct`, `*struct`, `map[string]any`) 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 ¶
type TXBuilder interface { // Wrap 包装查询选项 Wrap(opts ...SQLOption) SQLWrapper // contains filtered or unexported methods }
TXBuilder 事务构造器