🧰 Utility functions for working with []string in Go.
📦 Documentation: https://pkg.go.dev/github.com/shinagawa-web/strlistutils
Developers repeatedly re-implement the same []string routines (dedup, trim, filter). This package centralizes those patterns with clear behavior and tests, so teams can:
- avoid ad-hoc snippets scattered across services,
- keep behavior consistent and reviewable,
- get predictable performance without external deps.
- 🧹
RemoveDuplicates— remove duplicate strings - ✂️
TrimEach— trim whitespace from each element - ❌
FilterEmpty— remove empty strings - 🔁
Map— apply a function to each element - 🧪
Filter— filter elements by a predicate - 🔗
Join— join elements with a separator - ↩️
Reverse— reverse the list
go get github.com/shinagawa-web/strlistutils@v0.3.0🔒 It is recommended to pin a version to avoid unexpected breaking changes.
go get github.com/shinagawa-web/strlistutils@v0.3.0Pin a version to avoid accidental breakage.
package main
import (
"fmt"
"github.com/shinagawa-web/strlistutils"
)
func main() {
input := []string{" a ", "b", "a", " ", "c"}
out := strlistutils.RemoveDuplicates(
strlistutils.FilterEmpty(
strlistutils.TrimEach(input),
),
)
fmt.Println(out) // ["a", "b", "c"]
}➡️ For more examples, see GoDoc or examples
The package intentionally keeps a small surface area:
| Function | Behavior | |
|---|---|---|
RemoveDuplicates([]string) []string |
Deduplicate while preserving first appearance order. | |
TrimEach([]string) []string |
Trim leading/trailing spaces from each element. | |
FilterEmpty([]string) []string |
Remove empty strings. | |
Map([]string, func(string) string) []string |
Apply a mapper to each element. | |
Filter([]string, func(string) bool) []string |
Keep elements that satisfy the predicate. | |
Join([]string, string) string |
Join with a separator. | |
Reverse([]string) []string |
Reverse the order. | ([GitHub][1]) |
- No allocations beyond what the operation requires (e.g., dedup uses a set and preserves order).
- Benchmarks and fuzz tests are included in the repository to keep regressions visible in CI.
This library aims to be stable. Minor releases may add functions; breaking changes will bump the major version. Pin a tag in go.mod when adopting.
Typical use cases include CSV cleaning, environment variable parsing, and HTTP input normalization. A short write-up on the motivation and design is available on the project site/blog.
- Unit tests for all functions
- Benchmark tests
- Fuzz tests
- GitHub Actions CI
- Codecov integration
- Example code in examples/ directory
- Real-world use case: cleaning CSV input
MIT