Introduction
Lancet is a Go utility library inspired by JavaScript's Lodash and Java's Apache Commons. It provides a broad collection of helper functions organized into packages, covering common operations that Go developers would otherwise write repeatedly. It leverages Go generics for type-safe collection operations.
What Lancet Does
- Provides 600+ utility functions across 30+ packages for common Go tasks
- Covers string manipulation, slice/map operations, math, file I/O, and HTTP helpers
- Uses Go generics for type-safe collection functions like Filter, Map, Reduce, and GroupBy
- Includes concurrency utilities, retry logic, and rate limiting helpers
- Offers cryptography wrappers, random generation, and validation functions
Architecture Overview
Lancet is organized as a collection of focused packages within a single module. Each package (strutil, slice, maputil, fileutil, netutil, cryptoutil, etc.) is independent and can be imported individually. Functions are designed as pure, stateless utilities with no global state. Generics-based packages require Go 1.18+. The library has zero external dependencies — every function is implemented in pure Go.
Self-Hosting & Configuration
- Install with
go get github.com/duke-git/lancet/v2— requires Go 1.18+ - Import only the packages you need:
lancet/v2/slice,lancet/v2/strutil, etc. - No configuration required — all functions are stateless utilities
- Each package can be used independently without pulling in the entire library
- Full documentation available at pkg.go.dev with examples for every function
Key Features
- Zero dependencies — pure Go implementation with no external packages
- Generics support — type-safe collection operations without interface{} casting
- Comprehensive coverage — strings, slices, maps, math, datetime, file, net, crypto, system
- Well-tested — each function includes unit tests and benchmark tests
- Consistent API design — predictable naming and parameter conventions across packages
Comparison with Similar Tools
- Lodash (JS) — Lancet serves the same role for Go that Lodash serves for JavaScript
- samber/lo — another Go generics utility library; Lancet covers more domains beyond collections
- thoas/go-funk — pre-generics utility library using reflection; Lancet uses generics for type safety
- standard library — Go stdlib covers basics; Lancet fills gaps like CamelCase, Chunk, and Retry
FAQ
Q: Does Lancet require Go generics? A: The v2 branch requires Go 1.18+. A v1 branch exists for older Go versions but without generics-based functions.
Q: Will importing Lancet bloat my binary? A: No, Go only compiles the packages and functions you actually import and call.
Q: How stable is the API? A: Lancet follows semantic versioning. The v2 API is considered stable with backward-compatible additions.
Q: Can I contribute new utility functions? A: Yes, the project accepts contributions. New functions should include documentation, tests, and benchmarks.