Released a string slice utility for Go: “strlistutils”
Introduction
If you use Go, you probably handle []string more often than you might expect.
Parsing logs, formatting input data, processing values read from files, and so on…
But every time, having to write small bits of code for things like “removing duplicates”, “removing empty strings”, and “trimming spaces at the beginning and end” is a hassle.
To address this, I’ve released a small utility package, strlistutils, as OSS that bundles together commonly used operations.
Background
The Go standard library is simple and powerful, but it doesn’t provide many functions that conveniently handle string slices directly.
Common patterns include:
- Implementing it yourself with a for loop (you end up rewriting it every time)
- Using
map[string]struct{}together to remove duplicates (the code gets long) - Feeling reluctant to add a huge third-party utility just for a small operation
strlistutils fills this “small need”.
The focus is on being small, dependency-free, and simple.
Overview
-
🧰 Utility function set
A collection of basic operations for[]string -
📦 Zero dependencies
Safe to introduce into any project -
🧪 Extensive tests
Unit / Benchmark / Fuzz tests are provided
List of provided functions
| Function name | Description |
|---|---|
RemoveDuplicates |
Removes duplicates while preserving first-seen order |
TrimEach |
Removes leading and trailing spaces from each element |
FilterEmpty |
Removes empty strings |
Map |
Applies a function to each element |
Filter |
Keeps only elements that match a condition |
Join |
Joins elements with a separator |
Reverse |
Reverses the order of elements |
It’s simple, but it should cover most of the operations you find yourself “writing every time” in real-world work.
Installation
You can install it with a specific version:
go get github.com/shinagawa-web/strlistutils@v0.3.0
The library prioritizes stability, but to be safe, I recommend pinning a specific tag.
Usage example
For example, you can perform “whitespace trimming”, “duplicate removal”, and “empty string removal” all at once like this:
package main
import (
"fmt"
"github.com/shinagawa-web/strlistutils"
)
func main() {
input := []string{" a ", "b", "a", " ", "c"}
result := strlistutils.RemoveDuplicates(
strlistutils.FilterEmpty(
strlistutils.TrimEach(input),
),
)
fmt.Println(result) // Output: ["a", "b", "c"]
}
Of course, each function can be used on its own, but by combining them you can write data cleaning code concisely.
Quality assurance efforts
To ensure developers can use it with confidence, I’ve put a lot of effort into tests and quality checks.
- Unit tests: Tests are provided for all functions
- Benchmark tests: Performance can be verified
- Fuzz tests: Ensures robustness against unexpected inputs
- CI/CD: Automated tests via GitHub Actions and coverage visualization with Codecov
Looking for contributions
For the time being, the policy is to keep it “lightweight and stable”, but if you have requests like “I’d love a utility that does X!”, Issues and PRs are very welcome.
Summary
- Supplements the somewhat tedious
[]stringoperations not covered by the Go standard library - Small, simple, and dependency-free
- Quality is ensured through tests and CI
When you find yourself thinking “I’m writing the same processing code again” in your daily development, please give strlistutils a try.
Questions about this article 📝
If you have any questions or feedback about the content, please feel free to contact us.Go to inquiry form
Related Articles
Complete Cache Strategy Guide: Maximizing Performance with CDN, Redis, and API Optimization
2024/03/07Getting Started with Building Web Servers Using Go × Echo: Learn Simple, High‑Performance API Development in the Shortest Time
2023/12/03Go × Gin Advanced: Practical Techniques and Scalable API Design
2023/11/29Go × Gin Basics: A Thorough Guide to Building a High-speed API Server
2023/11/23Article & Like API with Go + Gin + GORM (Part 1): First, an "implementation-first controller" with all-in-one CRUD
2025/07/13Building an MVC-Structured Blog Post Web API with Go × Gin: From Basics to Scalable Design
2023/12/03Robust Test Design and Implementation Guide in a Go × Gin × MVC Architecture
2023/12/04Bringing a Go + Gin App Up to Production Quality: From Configuration and Structure to CI
2023/12/06