开源一个轻量级 Go 工具库:go-commons
项目背景
在日常 Go 开发中,我们经常需要处理字符串操作和系统监控相关的功能。虽然 Go 标准库提供了基础的字符串处理能力,但在实际项目中,我们往往需要一些更便捷的工具函数来提高开发效率。
基于"尽可能不使用第三方依赖"的原则,我开发了 go-commons 这个轻量级的 Go 工具库,专注于提供常用的字符串操作和系统工具函数。

项目特色
🎯 设计原则
- 零第三方依赖:优先使用 Go 标准库,避免依赖地狱
- API 简洁清晰:函数命名直观,参数设计合理
- 完整测试覆盖:每个函数都有对应的单元测试
- 中英文文档:提供完整的中英文 API 文档
📦 核心功能
字符串工具 (stringutils)
- 空值检查:IsEmpty、IsNotEmpty、IsBlank、IsNotBlank
- 字符串处理:Trim、Truncate、TruncateWithSuffix
- 大小写转换:Capitalize、Uncapitalize、ToUpperCase、ToLowerCase
- 字符串操作:ReverseString、ContainsAny、ContainsAll
- 子字符串处理:SubstringBefore、SubstringAfter
- 字符串连接:Join、Split
- 替换操作:Replace、ReplaceAll
- 填充功能:PadLeft、PadRight、Center
- 其他实用功能:Repeat、CountMatches、DefaultIfEmpty、DefaultIfBlank
系统工具 (systemutils)
- 预留了 cpuutils、memutils、diskutils目录结构
- 为后续添加系统监控功能做好准备

使用示例
安装
go get github.com/Rodert/go-commons
基础用法
package main
import (
    "fmt"
    "github.com/Rodert/go-commons/stringutils"
)
func main() {
    // 空值检查
    fmt.Println(stringutils.IsBlank("  \t\n"))         // true
    fmt.Println(stringutils.IsNotEmpty("hello"))      // true
    
    // 字符串处理
    fmt.Println(stringutils.Trim("  hello  "))       // "hello"
    fmt.Println(stringutils.Capitalize("hello"))     // "Hello"
    fmt.Println(stringutils.ReverseString("golang")) // "gnalog"
    
    // 截断和填充
    fmt.Println(stringutils.TruncateWithSuffix("abcdef", 4, "..")) // "ab.."
    fmt.Println(stringutils.PadLeft("42", 5, '0'))                // "00042"
    
    // 包含检查
    fmt.Println(stringutils.ContainsAny("gopher", "go", "java"))  // true
    
    // 默认值处理
    fmt.Println(stringutils.DefaultIfEmpty("", "default"))       // "default"
}
实际应用场景
1. 数据验证
// 检查用户输入
if stringutils.IsBlank(userInput) {
    return errors.New("输入不能为空")
}
// 设置默认值
displayName := stringutils.DefaultIfBlank(userName, "匿名用户")
2. 字符串格式化
// 格式化ID显示
formattedID := stringutils.PadLeft(id, 8, '0')
// 截断长文本
summary := stringutils.TruncateWithSuffix(longText, 100, "...")
3. 文本处理
// 提取域名
domain := stringutils.SubstringAfter(url, "://")
// 检查文件扩展名
if stringutils.EndsWith(filename, ".go") {
    // 处理Go文件
}
项目结构
go-commons/
├── stringutils/           # 字符串工具包
│   ├── stringutils.go    # 核心实现
│   └── stringutils_test.go # 单元测试
├── systemutils/          # 系统工具包(预留)
│   ├── cpuutils/         # CPU相关工具
│   ├── memutils/         # 内存相关工具
│   └── diskutils/        # 磁盘相关工具
├── examples/             # 使用示例
│   └── stringutils/
│       └── main.go       # 可运行示例
├── README.md             # 英文文档
├── README-zh.md          # 中文文档
├── LICENSE               # Unlicense许可证
└── go.mod                # Go模块定义
开源价值
🌟 为什么选择开源
- 社区贡献:希望为 Go 社区提供实用的工具函数
- 学习交流:通过开源项目与更多开发者交流经验
- 持续改进:社区反馈帮助项目不断完善
- 知识分享:分享 Go 开发的最佳实践
📈 项目优势
- 轻量级:无第三方依赖,体积小
- 易用性:API 设计简洁,学习成本低
- 可扩展:模块化设计,易于添加新功能
- 文档完善:中英文文档,支持 pkg.go.dev 展示
🚀 未来规划
- 完善系统工具:添加 CPU、内存、磁盘监控功能
- 增加更多示例:提供更多实际应用场景
- 性能优化:持续优化函数性能
- 社区建设:欢迎 Issue 和 PR 贡献
许可证
项目采用 Unlicense 许可证,完全开放,允许任何形式的商业和非商业使用。
贡献指南
欢迎任何形式的贡献:
- 🐛 Bug 报告:发现问题请提交 Issue
- 💡 功能建议:有新想法欢迎讨论
- 🔧 代码贡献:欢迎提交 Pull Request
- 📖 文档改进:帮助完善文档和示例
总结
go-commons 是一个专注于实用性的 Go 工具库,通过提供常用的字符串操作函数,帮助开发者提高开发效率。项目遵循"简单、实用、无依赖"的设计理念,适合在各种 Go 项目中使用。
如果你觉得这个项目有用,欢迎 Star 和 Fork,也欢迎提交 Issue 和 PR 来帮助项目不断完善!
项目地址:https://github.com/Rodert/go-commons
在线文档:https://pkg.go.dev/github.com/Rodert/go-commons
本文介绍了 go-commons 项目的设计理念、核心功能和使用方法,希望能为 Go 开发者提供一些参考和帮助。
go-commons
A zero-dependency Go utility kit for everyday development
Project Background
When writing Go code we all end up re-implementing the same small helpers:
“Is this string only whitespace?” “Pad this ID with zeros.” “Truncate and add an ellipsis.”
The standard library is great, but a concise, well-tested utility layer saves time and keeps projects consistent.
go-commons is that layer—no third-party dependencies, clear API, 100 % test coverage, bilingual docs.
Design Goals
- Zero dependencies – only the Go standard library
- Intuitive API – names you can guess without reading the docs
- Fully tested – every exported function has unit tests
- Bilingual docs – complete English & Chinese documentation
- Module-first – go getand use, no init steps
What’s Inside
stringutils
| Category | Examples | 
|---|---|
| Empty checks | IsEmpty,IsBlank,IsNotBlank | 
| Trimming | Trim,Truncate,TruncateWithSuffix | 
| Case | Capitalize,ToUpperCase,ToLowerCase | 
| Reverse / contains | ReverseString,ContainsAny,ContainsAll | 
| Substrings | SubstringBefore,SubstringAfter | 
| Join / split | Join,Split | 
| Replace | Replace,ReplaceAll | 
| Padding | PadLeft,PadRight,Center | 
| Misc | Repeat,CountMatches,DefaultIfEmpty,DefaultIfBlank | 
systemutils (placeholder structure)
cpuutils/, memutils/, diskutils/ – ready for future host-metrics helpers.
Quick Start
go get github.com/Rodert/go-commons
package main
import (
	"fmt"
	"github.com/Rodert/go-commons/stringutils"
)
func main() {
	// emptiness
	fmt.Println(stringutils.IsBlank("  \t\n"))     // true
	fmt.Println(stringutils.IsNotEmpty("hello"))  // true
	// trim & case
	fmt.Println(stringutils.Trim("  hello  "))   // "hello"
	fmt.Println(stringutils.Capitalize("hello")) // "Hello"
	// reverse
	fmt.Println(stringutils.ReverseString("golang")) // "gnalog"
	// truncate & pad
	fmt.Println(stringutils.TruncateWithSuffix("abcdef", 4, "..")) // "ab.."
	fmt.Println(stringutils.PadLeft("42", 5, '0'))                // "00042"
	// contains
	fmt.Println(stringutils.ContainsAny("gopher", "go", "java")) // true
	// default value
	fmt.Println(stringutils.DefaultIfEmpty("", "default")) // "default"
}
Real-World Recipes
1. Validate input
if stringutils.IsBlank(userInput) {
	return errors.New("input required")
}
name := stringutils.DefaultIfBlank(userName, "Anonymous")
2. Format identifiers
orderID := stringutils.PadLeft(strconv.Itoa(id), 8, '0')
3. Summaries
summary := stringutils.TruncateWithSuffix(article, 120, "…")
4. Quick parsing
domain := stringutils.SubstringAfter(url, "://")
if stringutils.HasSuffix(file, ".go") { /* … */ }
Repository Layout
go-commons/
├── stringutils/
│   ├── stringutils.go
│   └── stringutils_test.go
├── systemutils/
│   ├── cpuutils/
│   ├── memutils/
│   └── diskutils/
├── examples/
│   └── stringutils/
│       └── main.go
├── README.md
├── README-zh.md
├── LICENSE
└── go.mod
Why Open-Source?
- Give back to the Go community
- Learn from peer review
- Evolve faster with issues & PRs
- Share idiomatic Go patterns
Roadmap
License
The Unlicense – public domain, no restrictions, commercial or otherwise.
Contributing
All contributions welcome:
🐛 Open an issue for bugs
💡 Propose features in discussions
🔧 PRs must include tests & godoc comments
📖 Help with docs, examples, translations
Star & Share
If go-commons saves you time, please star the repo and spread the word!
Home: https://github.com/Rodert/go-commons
PkgDoc: https://pkg.go.dev/github.com/Rodert/go-commons
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号