Documentation ¶
Overview ¶
Package goutils 是一个将开发中常用的通用类函数封装统一存放在这里以便复用的函数工具包
Index ¶
- Variables
- func CloseGormInstances()
- func CloseRedisInstances()
- func CloseSqlxInstances()
- func CopyFile(sourceFile, destinationFile string) (err error)
- func GetLocalIP() (string, error)
- func GormMsSQL(which string) (*gorm.DB, error)
- func GormMySQL(which string) (*gorm.DB, error)
- func GormPostgres(which string) (*gorm.DB, error)
- func GormSQLite3(which string) (*gorm.DB, error)
- func InitViper(configPath, configName, configType string, ...) error
- func IsEqualStringSlice(s1, s2 []string) bool
- func IsInitedViper() bool
- func LikeFieldEscape(value string) string
- func NewGormMsSQL(conf DBConfig) (*gorm.DB, error)
- func NewGormMySQL(conf DBConfig) (*gorm.DB, error)
- func NewGormPostgres(conf DBConfig) (*gorm.DB, error)
- func NewGormSQLite3(conf DBConfig) (*gorm.DB, error)
- func NewRedisClient(opt *redis.Options) (*redis.Client, error)
- func NewRedisClusterClient(opt *redis.ClusterOptions) (*redis.ClusterClient, error)
- func NewRedisFailoverClient(opt *redis.FailoverOptions) (*redis.Client, error)
- func NewSqlxMsSQL(conf DBConfig) (*sqlx.DB, error)
- func NewSqlxMySQL(conf DBConfig) (*sqlx.DB, error)
- func NewSqlxPostgres(conf DBConfig) (*sqlx.DB, error)
- func NewSqlxSQLite3(conf DBConfig) (*sqlx.DB, error)
- func RedisClient(which string) (*redis.Client, error)
- func RedisClusterClient(which string) (*redis.ClusterClient, error)
- func RedisSentinelClient(which string) (*redis.Client, error)
- func RemoveAllWhitespace(s string) string
- func RemoveStringSliceItemByIndex(slice []string, index int) []string
- func RequestHTTPHandler(app http.Handler, method, path string, body []byte, header map[string]string) (*httptest.ResponseRecorder, error)
- func RequestHTTPHandlerFunc(f http.HandlerFunc, method string, body []byte, header map[string]string) ([]byte, error)
- func ReverseString(s string) string
- func SqlxMsSQL(which string) (*sqlx.DB, error)
- func SqlxMySQL(which string) (*sqlx.DB, error)
- func SqlxPostgres(which string) (*sqlx.DB, error)
- func SqlxSQLite3(which string) (*sqlx.DB, error)
- func StructToURLValues(i interface{}) (values url.Values)
- func URLKey(prefix, u string) string
- type DBConfig
- type ErrCode
- type GormBaseModel
- type Hashids
- type JSONTime
- type Pagination
Constants ¶
This section is empty.
Variables ¶
var GormInstances sync.Map
GormInstances 以 sync.Map 保存 gorm db 相关信息 key 为小写的数据库驱动名称, value 为实例名为 key , 具体的 db 对象为 value 的 sync.Map 形如: {"mysql": {"localhost": db}, "postgres": {"localhost": db}}
var (
JSONTimeFormat = "2006-01-02 15:04:05"
)
JSONTimeFormat 定义 JSONTime 的时间格式 该值可以被外部修改为指定的其他格式
var RedisClusterInstances sync.Map
RedisClusterInstances 按 which key 保存 redis cluster 客户端实例
var RedisInstances sync.Map
RedisInstances 按 which key 保存 redis 客户端实例
var RedisSentinelInstances sync.Map
RedisSentinelInstances 按 which key 保存 redis sentinel 客户端实例
var SqlxInstances sync.Map
SqlxInstances 以 sync.Map 保存 sqlx db 相关信息 key 为小写的数据库驱动名称, value 为实例名为 key , 具体的 db 对象为 value 的 sync.Map 形如: {"mysql": {"localhost": db}, "postgres": {"localhost": db}}
Functions ¶
func CloseGormInstances ¶
func CloseGormInstances()
CloseGormInstances 关闭全部的 Gorm 连接并重置 GormInstances
func CloseRedisInstances ¶
func CloseRedisInstances()
CloseRedisInstances 关闭全部的 redis 连接并重置 RedisInstances
func CloseSqlxInstances ¶
func CloseSqlxInstances()
CloseSqlxInstances 关闭全部的 Sqlx 连接并重置 SqlxInstances
func GormPostgres ¶
GormPostgres 根据 viper 配置中的实例名称返回 pg 实例
func GormSQLite3 ¶
GormSQLite3 根据 viper 配置中的实例名称返回 sqlite3 实例
func InitViper ¶
func InitViper(configPath, configName, configType string, onConfigChangeRun func(fsnotify.Event)) error
InitViper 根据配置文件路径和名称初始化 viper 并监听变化 configPath 配置文件路径 configName 配置文件名(不带格式后缀) configType 配置文件格式后缀 onConfigChangeRun 配置文件发生变化时的回调函数
func IsEqualStringSlice ¶
IsEqualStringSlice 判断两个 string slice 是否相同
func LikeFieldEscape ¶
LikeFieldEscape 转义 SQL 的 like 模糊查询时字段值为通配符的值
func NewGormMsSQL ¶
NewGormMsSQL 返回 gorm sqlserver 连接实例
func NewGormMySQL ¶
NewGormMySQL 返回 gorm mysql 连接实例
func NewGormPostgres ¶
NewGormPostgres 返回 gorm postgresql 连接实例
func NewGormSQLite3 ¶
NewGormSQLite3 返回 gorm sqlite3 连接实例
func NewRedisClient ¶
func NewRedisClient(opt *redis.Options) (*redis.Client, error)
NewRedisClient 返回 redis 的客户端连接对象
func NewRedisClusterClient ¶
func NewRedisClusterClient(opt *redis.ClusterOptions) (*redis.ClusterClient, error)
NewRedisClusterClient 返回 redis cluster 的连接对象
func NewRedisFailoverClient ¶
func NewRedisFailoverClient(opt *redis.FailoverOptions) (*redis.Client, error)
NewRedisFailoverClient 返回带 sentinel 的 redis 连接对象
func NewSqlxMsSQL ¶
NewSqlxMsSQL 返回 sqlx sqlserver 连接实例
func NewSqlxMySQL ¶
NewSqlxMySQL 返回 sqlx mysql 连接实例
func NewSqlxPostgres ¶
NewSqlxPostgres 返回 sqlx postgresql 连接实例
func NewSqlxSQLite3 ¶
NewSqlxSQLite3 返回 sqlx sqlite3 连接实例
func RedisClient ¶
RedisClient 根据 viper 配置返回 redis 客户端
func RedisClusterClient ¶
RedisClusterClient 根据 viper 配置返回 redis cluster 实例
func RedisSentinelClient ¶
RedisSentinelClient 根据 viper 配置返回 redis 客户端
func RemoveAllWhitespace ¶
RemoveAllWhitespace 删除字符串中所有的空白符
func RemoveStringSliceItemByIndex ¶
RemoveStringSliceItemByIndex 根据下标删除 string slice 中的元素
func RequestHTTPHandler ¶
func RequestHTTPHandler(app http.Handler, method, path string, body []byte, header map[string]string) (*httptest.ResponseRecorder, error)
RequestHTTPHandler 根据参数请求传入的 http.Handler 对应的 path 接口,用于接口测试 返回 ResponseRecorder: https://golang.org/pkg/net/http/httptest/#ResponseRecorder
func RequestHTTPHandlerFunc ¶
func RequestHTTPHandlerFunc(f http.HandlerFunc, method string, body []byte, header map[string]string) ([]byte, error)
RequestHTTPHandlerFunc 根据参数请求传入的 http.HandlerFunc 返回请求处理结果 body ,用于接口测试
func SqlxPostgres ¶
SqlxPostgres 根据 viper 中配置的实例名称返回 pg 实例
func SqlxSQLite3 ¶
SqlxSQLite3 根据 viper 中配置的实例名称返回 sqlite3 实例
func StructToURLValues ¶
StructToURLValues 将结构体指针对象转换为 url.Values , key 为 json tag , value 为结构体字段值,没有 json tag 则使用字段名称
Types ¶
type DBConfig ¶
type DBConfig struct { // DriverName 数据库名 DriverName string // Host 数据库 IP 地址 Host string // Port 数据库端口 Port int // Username 数据库用户名 Username string // Password 数据库密码 Password string // DBName 数据库名称 DBName string // LogMode 是否开启打印日志模式 LogMode bool // MaxIdleConns 设置空闲连接池中的最大连接数 MaxIdleConns int // MaxOpenConns 设置与数据库的最大打开连接数 MaxOpenConns int // ConnMaxLifeMinutes 设置可重用连接的最长时间(分钟) ConnMaxLifeMinutes int // ConnTimeout 连接超时时间(秒) ConnTimeout int // ReadTimeout 读超时时间(秒) ReadTimeout int // WriteTimeout 写超时时间(秒) WriteTimeout int // DisableSSL 是否关闭 ssl 模式 DisableSSL bool }
DBConfig 数据库配置
func (DBConfig) PostgresDSN ¶
PostgresDSN 返回 postgres dns 字符串
func (DBConfig) SQLite3DSN ¶
SQLite3DSN 返回 sqlite3 文件名
type ErrCode ¶
type ErrCode struct {
// contains filtered or unexported fields
}
ErrCode 错误码结构体
func NewErrCode ¶
NewErrCode 创建错误码,code 可以是任意类型
func (*ErrCode) AppendError ¶
AppendError 添加 err 到 errs 中
type GormBaseModel ¶
type GormBaseModel struct { ID string `gorm:"primary_key,column:id" json:"id" example:"-"` // 字符串类型的 Hash 主键 ID CreatedAt JSONTime `gorm:"column:created_at" json:"created_at" example:"-"` // 创建时间 UpdatedAt JSONTime `gorm:"column:updated_at" json:"updated_at" example:"-"` // 更新时间 }
GormBaseModel 基础 model 定义
type Hashids ¶
type Hashids struct { HashID *hashids.HashID HashIDData *hashids.HashIDData // contains filtered or unexported fields }
Hashids 封装hashids方法
func NewHashids ¶
NewHashids 创建Hashids对象 salt可以使用用户创建记录时的用户唯一身份标识+当前时间戳的字符串作为值 minLength指定转换后的最小长度,随着数字ID的增大长度可能会变长
type JSONTime ¶
JSONTime 用于在 json 中自定义时间格式 json marshal 一个带有 time.Time 字段类型的结构体时,时间格式固定为 RFC3339 格式 将 time.Time 类型替换为 JSONTime 类型,可设置时间格式为 JSONTimeFormat 中定义的格式
func (JSONTime) MarshalJSON ¶
MarshalJSON 使用 JSONTimeFormat 覆盖 time 包中实现的 json.Marshaler 接口
type Pagination ¶
type Pagination struct { // 数据总数 TotalCount int `json:"total_count"` // 分页总数 PagesCount int `json:"pages_count"` // 当前页码 PageNum int `json:"page_num"` // 分页大小 PageSize int `json:"page_size"` // 是否有上一页 HasPrev bool `json:"has_prev"` // 是否有下一页 HasNext bool `json:"has_next"` }
Pagination Paginate return it 异常数据时分页总数为 0 ,当前页码、上下页码均不判断逻辑,只管数值增减
func PaginateByOffsetLimit ¶
func PaginateByOffsetLimit(totalCount, offset, limit int) Pagination
PaginateByOffsetLimit 按 offset,limit 计算分页信息
func PaginateByPageNumSize ¶
func PaginateByPageNumSize(totalCount, pageNum, pageSize int) Pagination
PaginateByPageNumSize 按 pagenum,pagesize 计算分页信息 参数必须全部大于 0