go-kit

module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: Apache-2.0

README

Go-Kit

一个现代化、高性能的Go工具包,提供企业级应用开发所需的核心功能模块。

🚀 特性

  • 配置管理 - 基于Viper的灵活配置系统,支持多格式和环境变量
  • HTTP客户端 - 功能强大的HTTP客户端,支持重试、熔断、调试
  • 数据库连接 - 支持MySQL、PostgreSQL、SQLite,带重试机制
  • 日志记录 - 基于Zap的高性能结构化日志
  • 错误处理 - 统一的错误码系统和错误包装
  • HTTP服务器 - 基于Gin的轻量级HTTP服务器
  • 工具函数 - 常用工具函数和通用辅助能力

📦 模块概览

模块 描述 文档
config 配置管理系统 📖 详细文档
httpclient HTTP客户端 📖 详细文档
database 数据库连接管理 📖 详细文档
logger 日志记录系统 📖 详细文档
errors 错误处理系统 📖 详细文档
httpserver HTTP服务器 📖 详细文档
utils 工具函数 📖 详细文档
llm 大模型客户端统一封装(Eino 兼容) 📖 详细文档

🎯 快速开始

安装
go mod init your-project
go get github.com/spf13/viper
go get go.uber.org/zap
go get github.com/gin-gonic/gin
go get gorm.io/gorm
基本使用
package main

import (
    "log"
    "github.com/tsopia/go-kit/config"
    "github.com/tsopia/go-kit/logger"
    "github.com/tsopia/go-kit/httpclient"
)

// 配置结构
type AppConfig struct {
    Server struct {
        Host string `yaml:"host"`
        Port int    `yaml:"port"`
    } `yaml:"server"`
    Database struct {
        Host string `yaml:"host"`
        Port int    `yaml:"port"`
        Name string `yaml:"name"`
    } `yaml:"database"`
}

func main() {
    // 1. 加载配置
    var cfg AppConfig
    if err := config.LoadConfig(&cfg); err != nil {
        log.Fatal(err)
    }

    // 2. 初始化日志
    logger.SetupProduction()
    logger.Info("应用启动", "port", cfg.Server.Port)

    // 3. 创建HTTP客户端
    client := httpclient.NewClient()
    resp, err := client.Get("https://api.example.com/health")
    if err != nil {
        logger.Error("健康检查失败", "error", err)
    }

    logger.Info("应用运行中", "status", "ok")
}

📋 示例项目

查看 examples/ 目录获取完整的使用示例:

🏗️ 架构设计

模块化设计

每个功能模块都是独立的,可以单独使用或组合使用:

go-kit/
├── config/      # 配置管理
├── logger/      # 日志系统
├── httpclient/  # HTTP客户端
├── database/    # 数据库连接
├── errors/      # 错误处理
├── httpserver/  # HTTP服务器
├── utils/       # 工具函数
├── examples/    # 使用示例
└── docs/       # 详细文档
依赖关系
  • 所有模块都可以独立使用
  • 通过 utils 统一提供共享工具与公共辅助能力
  • 避免循环依赖,保持清晰的模块边界

🔧 环境要求

  • Go 1.21+
  • 支持的操作系统:Linux, macOS, Windows

📚 文档导航

核心模块
最佳实践

🤝 贡献

欢迎提交 Issue 和 Pull Request!

开发环境设置
git clone https://github.com/your-username/go-kit.git
cd go-kit
go mod tidy
go test ./...
代码规范
  • 遵循 Go 官方代码规范
  • 所有新功能需要包含测试
  • 提交前运行 go test ./...
  • 保持文档与代码同步

📄 许可证

MIT License - 详见 LICENSE 文件

🙏 致谢

感谢以下开源项目的支持:

  • Viper - 配置管理
  • Zap - 高性能日志
  • Gin - HTTP框架
  • GORM - ORM框架

Go-Kit - 让Go开发更简单、更高效 🚀

Directories

Path Synopsis
cmd
gokit module
Package config provides configuration management functionality.
Package config provides configuration management functionality.
cfg
Package cfg provides configuration management functionality.
Package cfg provides configuration management functionality.
examples
basic-config command
config-advanced command
config-demo command
database-retry command
database-simple command
errors-demo command
http-debug-test command
http-server command
httpclient-ctx command
kit command
modular-service command
pgmq-basic command
trace-test command
Package httpserver 提供基于 Gin 的 HTTP 服务封装。
Package httpserver 提供基于 Gin 的 HTTP 服务封装。
Package kit 提供基于标准库 slog 的日志封装,支持上下文提取、webhook 通知和堆栈跟踪。
Package kit 提供基于标准库 slog 的日志封装,支持上下文提取、webhook 通知和堆栈跟踪。
Package storage 提供对象存储统一封装。
Package storage 提供对象存储统一封装。

Jump to

Keyboard shortcuts

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