utils

Utils - The Missing Go Utilities Package

CodeQL Linter/Formatter Testing codecov Go Report Card Go Reference License: MIT Go Version

Status: Pre-release (approaching v0.1.0) - API may change

A comprehensive collection of utility packages for Go, designed to fill the gaps in the standard library and provide a consistent, well-tested set of tools for common programming tasks.

Overview

This project aims to provide a set of utility packages that follow these principles:

Package Structure

utils/
├── strutil/         # String manipulation & validation (currently in development)
├── httputil/        # HTTP client patterns
├── fileutl/         # File operations
├── jsonutil/        # JSON utilities
├── cryptoutil/      # Common crypto patterns
├── configutil/      # Configuration management
├── sliceutil/       # Generic slice operations
├── validationutil/  # Input validation
├── errorutil/       # Error handling
└── testutil/        # Test helpers

Recent Updates

The project has recently added several new features:

Roadmap

Upcoming features planned for future releases:

  1. Sanitization Functions: Enhanced sanitization for various data types and contexts
  2. Additional Comparison Functions:
    • Longest Common Subsequence (LCS)
    • Hamming Distance
    • Jaro and Jaro-Winkler Distance
    • Jaccard Similarity
    • Q-gram Distance
    • Sorensen-Dice Coefficient
    • Normalization function for edit distance
  3. Documentation Generation: GitHub Actions for automatic documentation and pages generation

Current Implementation

String Utilities (strutil)

The strutil package provides comprehensive string manipulation, validation, and sanitization functions. It offers both a functional API and a fluent builder pattern.

Error Constants

The package provides standardized error constants for validation failures:

// Error constants for validation
ErrInvalidEmail                   = "invalid email address"
ErrInvalidURL                     = "invalid URL"
ErrInvalidUUID                    = "invalid UUID"
ErrInvalidLengthRange             = "invalid length range"
ErrInvalidLength                  = "invalid length"
ErrInvalidEmpty                   = "empty string"
ErrInvalidEmptyAfterNormalization = "empty string after whitespace normalization"
ErrInvalidNotAlphaNumeric         = "string contains non-alphanumeric characters"

These constants are used throughout the package for consistent error messaging and can be checked when using the builder API’s validation methods.

Functional API

The functional API provides standalone functions for string operations:

// Generate a UUID
strutil.GenerateUUID()

// Validate an email
strutil.IsEmail(email)

// Create a URL-friendly slug
strutil.Slugify(rawText, 50)

// Keep only alphabetic characters
strutil.KeepAlpha(input, false)

// Keep only alphanumeric characters
strutil.KeepAlphaNumeric(input, true)

// HTML sanitization
strutil.StripHTML(input)
strutil.SanitizeHTML(input)

// Lorem ipsum generation
strutil.LoremWord()
strutil.LoremSentence()
strutil.LoremParagraph()
strutil.LoremEmail()
strutil.LoremURL()

StringBuilder API

The string builder API allows for chaining multiple operations:

// Chain multiple operations
strutil.New(input).
    CleanWhitespace().
    Truncate(100, "...").
    SanitizeHTML(allowedTags).
    String()

// Validation with error handling
strutil.New(input).
    RequireNotEmpty().
    RequireEmail().
    Result()

Implemented Functions (as of 7/16/2025)

The following functions have been fully implemented and are ready for use:

String Utilities (strutil)

UUID and Random String Generation

Lorem Ipsum Generation

String Validation

String Transformation

HTML and Sanitization

String Comparison

Version Utilities (version)

The BuildInfo struct also provides methods:

Installation

go get github.com/bmj2728/utils

Usage

Import the specific package you need:

import "github.com/bmj2728/utils/strutil"

Then use the functions or builders as needed:

// Using functional API
strutil.Slugify("Hello World!")

// Using string builder API
strutil.New(userInput).
    CleanWhitespace().
    RequireNotEmpty().
    Result()

Testing

This project follows Go’s standard testing practices. Each package includes comprehensive tests to ensure functionality, edge cases, and regression prevention.

Running Tests Locally

To run all tests in the project:

go test ./...

For verbose output:

go test -v ./...

To run tests for a specific package:

go test ./strutil

Continuous Integration

This project uses GitHub Actions for continuous integration. The workflow automatically runs:

  1. Linting & Formatting - Using golangci-lint, go vet, and go fmt to ensure code quality and consistent formatting
  2. Testing - Running all tests across multiple Go versions (1.22, 1.23, 1.24) with race detection
  3. Security Scanning - Using gosec, govulncheck, and dependency review to identify security issues
  4. Building - Building for multiple platforms (Linux, macOS, Windows) and architectures (amd64, arm64)

The CI/CD workflows run on all pull requests and pushes to the main branch, ensuring code quality, security, and functionality are maintained. Weekly security scans are also scheduled to catch newly discovered vulnerabilities.

CI/CD Workflow Details (as of 7/16/2025)

All workflows use concurrency groups to avoid redundant runs and optimize CI/CD resources.

Acknowledgements

This project leverages several excellent open-source libraries:

License

This project is licensed under the terms of the LICENSE file included in the repository.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.