go-kit

module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: Apache-2.0

README

Go-Kit

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

🚀 特性

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

📦 模块概览

模块 描述 文档
pkg/config 配置管理系统 📖 详细文档
pkg/httpclient HTTP客户端 📖 详细文档
pkg/database 数据库连接管理 📖 详细文档
pkg/logger 日志记录系统 📖 详细文档
pkg/errors 错误处理系统 📖 详细文档
pkg/httpserver HTTP服务器 📖 详细文档
pkg/constants 常量定义 📖 详细文档
pkg/utils 工具函数 📖 详细文档

🎯 快速开始

安装
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"
    "go-kit/pkg/config"
    "go-kit/pkg/logger"
    "go-kit/pkg/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/
├── pkg/
│   ├── config/      # 配置管理
│   ├── logger/      # 日志系统  
│   ├── httpclient/  # HTTP客户端
│   ├── database/    # 数据库连接
│   ├── errors/      # 错误处理
│   ├── httpserver/  # HTTP服务器
│   ├── constants/   # 常量定义
│   └── utils/       # 工具函数
├── examples/        # 使用示例
└── docs/           # 详细文档
依赖关系
  • 所有模块都可以独立使用
  • 通过 pkg/constants 解决共享常量问题
  • 避免循环依赖,保持清晰的模块边界

🔧 环境要求

  • 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
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
modular-service command
trace-test command

Jump to

Keyboard shortcuts

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