gokit

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 0 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gokit

A batteries-included standard-library companion for Go — the Swiss Army knife that Java developers know as Guava and JavaScript developers know as Lodash.

gokit is a collection of small, focused, generics-first packages that fill the gaps in Go's standard library: slices, maps, strings, math, time, crypto, I/O, networking, concurrency, retries, structured logging, and HTTP plumbing. Every package is independent — import only what you need, and pull in only the dependencies that package requires.

import "github.com/tmoeish/gokit/slicex"

evens := slicex.Filter([]int{1, 2, 3, 4}, func(n int) bool { return n%2 == 0 })
// []int{2, 4}

Install

go get github.com/tmoeish/gokit@latest

Requires Go 1.22+ (uses generics and math/rand/v2).

Design principles

  • One concern per package. Each xxx package owns one domain and uses the conventional x suffix (slicex, mapx, strx, …) to avoid clashing with the std-lib package it complements.
  • Generics over interface{}. Functions are type-safe; no reflection on hot paths.
  • Pure functions, no surprises. Helpers return new values instead of mutating inputs unless the name says otherwise (e.g. Fill).
  • Errors are values, panics are opt-in. Fallible helpers return error; the must package and Must* variants exist for the cases where panicking is what you actually want.
  • Minimal dependencies. The only third-party deps are google/uuid, yaml.v3, and labstack/echo/v4 — and the last is only pulled in if you import echox.

Packages

Package What it gives you Inspired by
slicex Map/Filter/Reduce, set ops, chunk/window/zip, sampling, min/max-by samber/lo, lancet, Guava
mapx Keys/Values/Entries, Merge, Pick/Omit, Invert, MapValues, GroupBy samber/lo, lancet
setx Generic hash Set[T]: union/intersection/difference, subset/superset Guava Sets, lancet
strx Blank checks, case conversion (camel/snake/kebab/pascal), slugify, pad, between Guava Strings, lancet
mathx Clamp, Abs, Sum/Average, GCD/LCM, Pow, prime/fib, rounding, safe divide lancet, Guava math
conv Safe any → string/int/float/bool conversions with explicit errors spf13/cast
timex Begin/End of day/week/month/year, work-days, age, durations, parsing jinzhu/now, lancet
randx Random strings/charsets, ints, UUIDs, weighted choice, sampling lancet, Guava
cryptox MD5/SHA/HMAC hashing, base64/hex (encode + verify) std crypto wrappers
iox File read/write/append, lines, exists, copy, temp files, ensure-dir lancet fileutil
jsonx JSON + YAML marshal/unmarshal, pretty-print, deep clone, path Get std encoding wrappers
netx Local IPs, IP/CIDR validation, free ports, host:port parsing lancet netutil
ptrx Of/Deref pointer helpers, nil-coalescing AWS aws.String, samber/lo
must Must/OK/Assert panic-on-error helpers samber/lo Must
retry Context-aware retries with constant/linear/exponential backoff + jitter avast/retry-go, cenkalti/backoff
syncx SafeMap, generic Once[T], WaitGroupCtx, Notifier std sync, samber/lo
contextx Type-safe context values; package-defined request metadata helpers std context
logx Structured logging on log/slog with context propagation + caller/stack helpers std log/slog
httpx Framework-agnostic response envelope, application error codes, pagination —
echox Wires httpx + contextx into labstack/echo (responses, error handler, mw) —

Full, runnable API docs live on pkg.go.dev/github.com/tmoeish/gokit.

Examples

slicex — transform and aggregate
people := []Person{{"Ann", 30}, {"Bob", 25}, {"Cy", 30}}

byAge := slicex.GroupBy(people, func(p Person) int { return p.Age })
names := slicex.Map(people, func(p Person) string { return p.Name })
oldest, _ := slicex.MaxBy(people, func(p Person) int { return p.Age })
setx — set algebra
a := setx.New(1, 2, 3)
b := setx.New(2, 3, 4)
a.Intersection(b).ToSlice() // [2 3]
a.Union(b).Len()            // 4
a.IsSubsetOf(b)             // false
retry — resilient calls
err := retry.Do(ctx, func() error {
    return callFlakyAPI()
},
    retry.WithMaxAttempts(5),
    retry.WithExponentialBackoff(100*time.Millisecond, 2.0),
    retry.WithJitter(0.2),
)
echox — HTTP responses for echo
e := echo.New()
e.HTTPErrorHandler = echox.ErrorHandler
e.Use(echox.RequestID())

e.GET("/users/:id", func(c echo.Context) error {
    u, err := store.Find(c.Param("id"))
    if err != nil {
        return echox.Error(c, httpx.CodeRecordNotFound, err)
    }
    return echox.Success(c, u)
})

Project layout

The repository follows Effective Go and the standard Go module conventions: a flat set of single-purpose packages at the module root, each in its own directory with co-located *_test.go files. There is no pkg/ or src/ nesting — import paths read as github.com/tmoeish/gokit/<pkg>.

gokit/
├── slicex/      mapx/      setx/     strx/      mathx/
├── conv/        timex/     randx/    cryptox/   iox/
├── jsonx/       netx/      ptrx/     must/      retry/
├── syncx/       contextx/  logx/     httpx/     echox/
├── go.mod
├── Makefile         # dev harness: make check / test / lint / fmt
├── AGENTS.md        # rules for humans and coding agents
└── README.md

Contributing & developing

Run the full local check (format, vet, lint, test with race + coverage) before sending changes:

make check

See AGENTS.md for the conventions every change must follow and CONTRIBUTING.md for the workflow. The same rules are encoded as a Claude Code skill under .claude/skills/gokit/ so coding agents pick them up automatically.

License

MIT

Documentation

Overview

Package gokit is the module root for github.com/tmoeish/gokit, a batteries-included companion to the Go standard library — the Swiss Army knife that Java calls Guava and JavaScript calls Lodash.

gokit is a collection of small, focused, generics-first packages. Import only the ones you need:

  • slicex — generic slice utilities (map/filter/reduce, set ops, chunking)
  • mapx — generic map utilities (keys/values, merge, pick/omit, invert)
  • setx — a generic hash Set with union/intersection/difference
  • strx — string helpers (case conversion, slugify, blank checks, pad)
  • mathx — generic math (clamp, gcd/lcm, rounding, primes, safe divide)
  • conv — safe any → string/number/bool conversions with explicit errors
  • timex — time helpers (begin/end of period, work-days, age, parsing)
  • randx — random strings, ints, UUIDs, weighted choice, sampling
  • cryptox — hashing (MD5/SHA/HMAC) and base64/hex encoding
  • iox — file and I/O helpers (read/write lines, exists, copy, temp)
  • jsonx — JSON and YAML marshal/unmarshal, pretty-print, deep clone
  • netx — network helpers (local IPs, IP/CIDR validation, free ports)
  • ptrx — pointer helpers (Of/Deref, nil-coalescing)
  • must — Must/OK/Assert panic-on-error helpers
  • retry — context-aware retries with configurable backoff
  • syncx — concurrent structures (SafeMap, generic Once, WaitGroupCtx)
  • contextx — type-safe context values and request metadata helpers
  • logx — structured logging on log/slog with context propagation
  • httpx — framework-agnostic HTTP response envelope and error codes
  • echox — bridges httpx + contextx into the labstack/echo framework

This root package intentionally exposes no symbols; see the subpackages.

Directories

Path Synopsis
Package contextx provides context utilities for attaching and retrieving typed values from a context.Context.
Package contextx provides context utilities for attaching and retrieving typed values from a context.Context.
Package conv provides safe type conversion utilities.
Package conv provides safe type conversion utilities.
Package cryptox provides cryptographic utility functions.
Package cryptox provides cryptographic utility functions.
Package echox bridges gokit's framework-agnostic httpx and contextx packages into the labstack/echo web framework.
Package echox bridges gokit's framework-agnostic httpx and contextx packages into the labstack/echo web framework.
Package httpx provides HTTP response types, status codes, and helpers.
Package httpx provides HTTP response types, status codes, and helpers.
Package iox provides I/O utility functions.
Package iox provides I/O utility functions.
Package jsonx provides JSON and YAML utility functions.
Package jsonx provides JSON and YAML utility functions.
Package logx provides structured logging utilities built on top of log/slog.
Package logx provides structured logging utilities built on top of log/slog.
Package mapx provides generic map utilities.
Package mapx provides generic map utilities.
Package mathx provides generic math utilities.
Package mathx provides generic math utilities.
Package must provides must-succeed helpers that panic on error.
Package must provides must-succeed helpers that panic on error.
Package netx provides network utility functions.
Package netx provides network utility functions.
Package ptrx provides pointer utilities for working with Go generics.
Package ptrx provides pointer utilities for working with Go generics.
Package randx provides random utilities.
Package randx provides random utilities.
Package retry provides retry utilities with configurable backoff strategies.
Package retry provides retry utilities with configurable backoff strategies.
Package setx provides a generic, hash-based Set collection.
Package setx provides a generic, hash-based Set collection.
Package slicex provides generic slice utilities.
Package slicex provides generic slice utilities.
Package strx provides string utilities.
Package strx provides string utilities.
Package syncx provides concurrent data structures and utilities.
Package syncx provides concurrent data structures and utilities.
Package timex provides time utilities.
Package timex provides time utilities.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL