項目背景
在日常 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 mainimport ("fmt""github.com/Rodert/go-commons/stringutils"
)func main() {// 空值檢查fmt.Println(stringutils.IsBlank(" \t\n")) // truefmt.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 get
and 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 mainimport ("fmt""github.com/Rodert/go-commons/stringutils"
)func main() {// emptinessfmt.Println(stringutils.IsBlank(" \t\n")) // truefmt.Println(stringutils.IsNotEmpty("hello")) // true// trim & casefmt.Println(stringutils.Trim(" hello ")) // "hello"fmt.Println(stringutils.Capitalize("hello")) // "Hello"// reversefmt.Println(stringutils.ReverseString("golang")) // "gnalog"// truncate & padfmt.Println(stringutils.TruncateWithSuffix("abcdef", 4, "..")) // "ab.."fmt.Println(stringutils.PadLeft("42", 5, '0')) // "00042"// containsfmt.Println(stringutils.ContainsAny("gopher", "go", "java")) // true// default valuefmt.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
- Host-metrics helpers (CPU, memory, disk)
- More examples & benchmarks
- Performance tuning
- Community guidelines & contributor covenant
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