A comprehensive collection of Go utility functions and packages designed for modern Go development (Go 1.24+). Features performance-optimized implementations, type-safe generics, and comprehensive testing.
For detailed usage examples, please refer to the test cases in each package.
DoWithWorker() |
execute function with worker pool (context-aware) |
Every() |
run function at regular intervals (context-aware) |
After() |
run function after delay (context-aware) |
Async(),Async2() |
run function asynchronously and receive result via channel |
// Worker pool with optimized performance
err := DoWithWorker(ctx, 4, func(ctx context.Context, workerID int) error {
fmt.Printf("Worker %d processing\n", workerID)
return processJob(ctx)
})
// Periodic execution
err := Every(ctx, 5*time.Second, true, func(ctx context.Context) {
updateMetrics(ctx)
})
// Asynchronous execution
resultCh := Async(ctx, func(ctx context.Context) string {
return fetchData(ctx)
})
result := <-resultChReadJSON[T] |
decode JSON to type with generics |
WriteJSON[T] |
encode type to JSON writer |
MustMarshalJson() |
marshal JSON or panic |
ReadXML[T] |
decode XML to type with generics |
WriteXML[T] |
encode type to XML writer |
ReadYAML[T] |
decode YAML to type with generics |
WriteYAML[T] |
encode type to YAML writer |
// Type-safe JSON operations
var config Config
err := ReadJSON[Config](reader, &config)
// Must marshal - panic on error
payload := MustMarshalJson(data)
// Direct type inference
users, err := ReadJSON[[]User](reader)SetBits() |
set bits in integer |
ClearBits() |
clear bits in integer |
IsContextDone() |
return true if context is done |
WithTimeout() |
run fn() with timeout context |
Must(error) |
panic if error not nil |
NewPool[T]() |
sync.Pool with generics |
Abs[T]() |
absolute value with generics |
Timer() |
measure execution time |
// Generic pool for any type
pool := NewPool[*bytes.Buffer](func() *bytes.Buffer {
return &bytes.Buffer{}
})
// Execution timing
defer Timer("operation")()
// Output: time takes 123.45ms: operationAtoiDef[T]() |
strconv.Atoi() with default value |
ParseBoolDef() |
strconv.ParseBool() with default value |
ParseIntDef[T]() |
strconv.ParseInt() with default value |
// Safe parsing with defaults
port := AtoiDef(os.Getenv("PORT"), 8080) // defaults to 8080
timeout := ParseIntDef[int64](cfg.Timeout, 30) // defaults to 30
debug := ParseBoolDef(os.Getenv("DEBUG"), false) // defaults to falseParseDateTime() |
parse string to time for well known layouts |
TimeWithLayout |
time.Time with layouts |
RFC1123ZTime |
Mon, 02 Jan 2006 15:04:05 -0700 |
RFC3339Time |
2006-01-02T15:04:05Z07:00 |
// Smart time parsing
t, err := ParseDateTime("2023-12-25T10:30:00Z") // Auto-detects format
rfc3339 := RFC3339Time(time.Now()) // Formats as RFC3339
rfc1123 := RFC1123ZTime(time.Now()) // Formats as RFC1123ZSecurity Warning: Be careful with shell command execution. Always validate and sanitize inputs to prevent command injection attacks.
// Run command and output to stdin/stdout
exc := Exec("ls", "-al")
err := exc.Do(context.Background())
require.NoError(t, err)
// Run command and get output
exc := Exec("ls", "-al")
output, err := exc.Output(context.Background())
require.NoError(t, err)
require.Contains(t, string(output), "README.md")IfThen(true, func() { fmt.Printf("true\n") })
// Output: true
IfThen(condition,
func() { fmt.Printf("true\n") },
func() { fmt.Printf("false\n") })
// Multiple else conditions supported
IfThen(false,
func() { fmt.Printf("condition1\n") },
func() { fmt.Printf("condition2\n") },
func() { fmt.Printf("default\n") })// Simple ternary
result := Ternary(condition, "yes", "no")
// Function-based ternary (lazy evaluation)
result := TernaryF(condition,
func() string { return expensiveTrue() },
func() string { return expensiveFalse() })Security: Uses cryptographically secure random generation with input validation.
b := RandomByte(10)
// hex.EncodeToString(b) = "4d46ef2f87b8191daf58"s := RandomString(10)
// s = "$c&I$#LR3Y"
// Custom character set with validation
s := RandomStringWith(10, []rune("abcdefg"))
// s = "bbffedabda"
// Size limits enforced for security (max 1MB)
s := RandomStringWith(1000, []rune("0123456789"))func doSomething() {
defer Timer("doSomething()")()
time.Sleep(500 * time.Millisecond)
}
doSomething()
// Output: time takes 500.505063ms: doSomething()Tuple2, Tuple3 |
Pack() and Unpack() |
T2,T3 |
construct tuple with element |
// Create and unpack tuples
tuple := T2("hello", 42)
str, num := tuple.Unpack()
// Multi-return functions
func getData() (string, int, error) {
return "data", 123, nil
}
result := T3(getData())- requests - Simple HTTP client with connection pooling and buffer optimization
resp, err := requests.Get("https://api.example.com"). Header("Authorization", "Bearer token"). JSON(payload). Do(ctx)
- slicex - Performance-optimized slice operations with generics and
iter.Seqsupport - mapx - Map extensions with functional operations and optimized sampling
- sets - Set data structure implementation with
SetNX()support - chanx - Channel extensions and utilities
- fx - Experimental functional programming with
iter.Seq(Go 1.24+) - fx/gen - Generator functions and utilities
- iterx - Iterator extensions and helper functions
- cryptox - Encryption/decryption functions (DES deprecated, use AES)
- x509x - X.509 certificate utilities
- log - Simple structured logging powered by Zap
- errors - Errors with stack traces and rich formatting
- retry - Retry mechanisms with backoff strategies
- services - Simple service framework with lifecycle management
- testx - Unit test utility functions
- fixtures - Test fixture management (env vars, files)
- httptest - HTTP test server utilities
- validate - Struct validation made easy
- cobrax - Cobra command utilities with simplified command creation
// Create root command rootCmd := cobrax.Add(nil, &cobra.Command{Use: "app"}, nil) // Add subcommand with configuration cobrax.Add(rootCmd, &cobra.Command{Use: "sub"}, func(cmd *cobra.Command) { cmd.Flags().StringP("name", "n", "", "name flag") })
- flags - Command-line flag management
- slug - UUID to slug conversion
Recent performance improvements include:
- HTTP Client: Connection pooling, buffer reuse, optimized timeouts
- Sampling Operations: 100x performance improvement using
math/randinstead ofcrypto/rand - Algorithm Efficiency: Fisher-Yates shuffle for O(n) complexity
- Memory Management: Buffer pooling and reduced allocations
- Type Safety: Extensive use of Go generics for zero-cost abstractions
Security Features:
- Input validation and size limits in random string generation
- Secure logging practices (no sensitive data in logs)
- Deprecation warnings for insecure cryptographic functions
- Command injection prevention guidance
Security Warnings:
- Always validate shell command inputs
- DES encryption is deprecated - use AES instead
- Be cautious with command execution functions