Released a string slice utility for Go: “strlistutils”

  • opensource
    opensource
  • golang
    golang
Published on 2025/06/19

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.

https://pkg.go.dev/github.com/shinagawa-web/strlistutils

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.

https://github.com/shinagawa-web/strlistutils/issues

Summary

  • Supplements the somewhat tedious []string operations 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.

https://github.com/shinagawa-web/strlistutils

https://pkg.go.dev/github.com/shinagawa-web/strlistutils

Xでシェア
Facebookでシェア
LinkedInでシェア

Questions about this article 📝

If you have any questions or feedback about the content, please feel free to contact us.
Go to inquiry form