global

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DriverTypeMemory = "go_memory"
	DriverTypeRedis  = "redis"
	DriverTypeMySQL  = "mysql"
	DriverTypeSQLite = "sqlite"
)

全局定义,禁止代码中直接写字符串比较,如("go_memory" == driverType) 而应使用 global.DriverTypeMemory, global.IsActiveDriverType(global.DriverTypeMemory) 等

Variables

This section is empty.

Functions

func CamelCaseToSnakeCase

func CamelCaseToSnakeCase(camel string) string

CamelCaseToSnakeCase 驼峰 -> 蛇形命名 示例:

  • CamelCaseToSnakeCase("PasswordEntry") // "password_entry"
  • CamelCaseToSnakeCase("UserName") // "user_name"

func CloseMySQL

func CloseMySQL() error

CloseMySQL 关闭MySQL全局实例

func CloseSQLite

func CloseSQLite() error

CloseSQLite 关闭SQLite全局实例

func GetCurrentDriverType

func GetCurrentDriverType() string

GetCurrentDriverType 获取当前驱动类型

func GetMySQL

func GetMySQL() *sql.DB

GetMySQL 获取MySQL全局实例

func GetSQLite

func GetSQLite() *sql.DB

GetSQLite 获取SQLite全局实例

func InitMySQL

func InitMySQL(dsn string) (*sql.DB, error)

InitMySQL 初始化MySQL全局实例

func InitMySQLFromDB

func InitMySQLFromDB(db *sql.DB) error

InitMySQLFromDB 使用已有的数据库连接初始化全局MySQL实例

func InitSQLiteFromDB

func InitSQLiteFromDB(db *sql.DB) error

InitSQLiteFromDB 使用已有的数据库连接初始化全局SQLite实例

func IsActiveDriverType

func IsActiveDriverType(driverType string) bool

IsActiveDriverType 检查是否是当前活跃驱动类型

func IsInitializedDriverType

func IsInitializedDriverType(driverType string) bool

IsInitializedDriverType 检查驱动是否已初始化

func IsMySQLInitialized

func IsMySQLInitialized() bool

IsMySQLInitialized 检查MySQL是否已初始化

func IsSQLiteInitialized

func IsSQLiteInitialized() bool

IsSQLiteInitialized 检查SQLite是否已初始化

func Jsondump

func Jsondump(data any)

func SetCurrentDriverType

func SetCurrentDriverType(driverType string)

SetCurrentDriverType 设置当前驱动类型

func SnakeCaseToCamelCase

func SnakeCaseToCamelCase(snake string) string

SnakeCaseToCamelCase 蛇形 -> 大驼峰命名 示例:

  • SnakeCaseToCamelCase("password_entry") // "PasswordEntry"
  • SnakeCaseToCamelCase("user_name") // "UserName"

func SnakeCaseToCamelCaseBak

func SnakeCaseToCamelCaseBak(snake string) string

SnakeCaseToCamelCase 蛇形 -> 大驼峰命名(支持混合大小写输入,兼容纯小写蛇形命名) 处理示例:

  • 1. indexLOGIN -> Indexlogin
  • 2. user_name -> UserName
  • 3. GOODS_PRICE -> GoodsPrice
  • 4. test_123_ABC -> Test123Abc

func SnakeToLowerCamel

func SnakeToLowerCamel(s string) string

大驼峰 -> 小驼峰命名 示例:

  • SnakeToLowerCamel("PasswordEntry") // "passwordEntry"
  • SnakeToLowerCamel("user_name") // "userName"

func Substring

func Substring(str string, start, length int) string

Substring 截取字符串(支持中文等多字节字符) 示例:

  • Substring("密码本系统", 0, 2) // "密码"
  • Substring("hello world", 6, 11) // "world"

Types

type Except

type Except struct {
	Message string `json:"message" xml:"message"` // 异常描述信息
	Code    int    `json:"code" xml:"code"`       // 错误码
	File    string `json:"file" xml:"file"`       // 调用文件完整路径
	Fn      string `json:"func" xml:"func"`       // 调用函数全名(带包名,如 "business/user.GetUser")
	Entry   string `json:"entry" xml:"entry"`
}

Except 异常结构体,包含函数名、文件、行号等核心信息

func Exception

func Exception() Except

Exception 生成默认异常(message="exception",code=400)

  • deepIndex:可选参数,指定栈帧偏移补偿(适配不同调用栈层级,默认0)

func ExceptionWith

func ExceptionWith(message string, args ...int) Except

ExceptionWith 自定义异常信息和错误码,适配任意调用栈层级 参数说明:

  1. message:必传,异常描述
  2. code:可选,错误码(默认400)
  3. deepIndex:可选,栈帧偏移补偿值(用于适配不同调用栈深度,默认0定位业务调用侧)

func (Except) Error

func (e Except) Error() error

Jump to

Keyboard shortcuts

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