This repository contains shared Go libraries and utilities used across Ampersand projects. It provides a collection of reusable packages for common functionality like actor models, object pooling, concurrent execution, environment variable parsing, telemetry, and more.
amp-common is a Go library (not a standalone application) that provides shared utilities and packages. It uses Go 1.25.5 and is published as github.com/amp-labs/amp-common.
- Go 1.25.5+
- SSH Key Setup: This project uses private GitHub repositories. You need to set up SSH authentication:
-
Ensure you have an SSH key configured for GitHub access to private amp-labs repositories
-
Configure git to use SSH for GitHub:
git config --global url."git@github.com:".insteadOf "https://github.com/"
-
Set the GOPRIVATE environment variable (add to your shell profile):
export GOPRIVATE=github.com/amp-labs/*
-
make test # Run all tests
go test -v ./... # Run tests with verbose output
go test -v -run TestName ./package-name # Run a specific testmake fix # Run all formatters and linters with auto-fix
make fix/sort # Same as fix but with sorted output
make lint # Run linters without auto-fix
make format # Alias for 'make fix'
make fix-markdown # Fix markdown filesThe linting stack includes:
wsl- Whitespace linter (allows cuddle declarations)gci- Go import formattergolangci-lint- Comprehensive Go linter (configured via.golangci.yml)
actor - Actor model implementation with message passing
- Generic
Actor[Request, Response]with concurrent message processing - Actors have mailboxes (channels) and process messages sequentially
- Includes Prometheus metrics for monitoring
- Methods:
Send,SendCtx,Request,RequestCtx,Publish,PublishCtx - Graceful panic recovery
pool - Generic object pooling with lifecycle management
- Thread-safe pool for any
io.Closerobjects - Dynamic growth, configurable idle cleanup
- Prometheus metrics for monitoring
- Uses channels and semaphores for concurrency control
simultaneously - Parallel execution utility
Do(maxConcurrent int, ...func(context.Context) error)- Run functions in parallel- Returns first error encountered, cancels remaining on error
- Automatic panic recovery with stack traces
- Semaphore-based concurrency limiting
envutil - Type-safe environment variable parsing
- Fluent API with
Reader[T]type for chaining operations - Built-in support for: strings, ints, bools, durations, URLs, UUIDs, file paths, etc.
- Options pattern:
Default(),Required(),Validate(), etc. - Example:
envutil.Int("PORT", envutil.Default(8080)).Value()
telemetry - OpenTelemetry tracing integration
Initialize(ctx, config)- Set up OTLP tracingLoadConfigFromEnv()- Load config from environment variables- Auto-detects Kubernetes environments and uses cluster-local collector
- Environment variables:
OTEL_ENABLED,OTEL_SERVICE_NAME,OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
logger - Structured logging utilities
- Built on Go's
slogpackage - Integrates with OpenTelemetry context
cli - CLI utilities for terminal interaction
- Banner/divider generation with Unicode box drawing
BannerAutoWidth(),DividerAutoWidth()- Auto-detect terminal size- Prompt utilities for user input
- Set
AMP_NO_BANNER=trueto suppress banners
cmd - Command execution wrapper
- Fluent API for building
exec.Cmdinstances - Methods:
SetDir(),SetStdin(),SetStdout(),SetStderr(),AppendEnv(), etc. - Returns exit code and error separately
maps- Generic map utilities with red-black tree implementationset- Generic set implementation with red-black tree backingtuple- Generic tuple typescollectable- Interface combiningHashableandComparablefor use in Map/Set data structuressortable- Sortable interface withLessThancomparison for ordering
closer- Resource management utilities forio.Closer(Closercollector,CloseOnce,HandlePanic,CustomCloser)using- Resource management pattern (try-with-resources/using statement)should- Utilities for cleanup operations (e.g.,should.Close())shutdown- Graceful shutdown coordination
retry- Flexible retry mechanism with exponential backoff, jitter, and retry budgetserrors- Error utilities with collection supporttry- Result type for error handling (Try[T]withValueandError)
jsonpath- JSONPath bracket notation utilities for field mappingxform- Type transformations and conversionshashing- Hashing utilitiessanitize- String sanitizationcompare- Comparison utilitieszero- Zero value utilities for generic types (Value[T](),IsZero[T](value))
future- Future/Promise implementation for async programming (Go,GoContext,Await,Map,Combine)bgworker- Background worker managementlazy- Lazy initialization with thread-safety
optional- Type-safe Optional/Maybe type (Some[T],None[T],Map,FlatMap)pointer- Pointer utilities (To[T],Value[T])
utils- Misc utilities (channels, context, JSON, sleep, dedup)channels- Channel utilities (CloseChannelIgnorePanic)contexts- Context utilities (EnsureContext,IsContextAlive,WithValue[K,V],GetValue[K,V])envtypes- Common environment variable types (HostPort, Path)emoji- Emoji constants for terminal output and UI (Rocket, Fire, ThumbsUp, Warning, etc.)stage- Environment detection (local, test, dev, staging, prod)script- Script execution utilitiesbuild- Build information utilitieshttp/transport- HTTP transport configuration with DNS cachingassert- Assertion utilities for testingdebug- Debugging utilities (for local development only, not for production use)
This is a Go module (github.com/amp-labs/amp-common). When changes are pushed to main, Cloud Build automatically:
- Creates a PR in the
serverrepository to update theamp-commondependency - Closes old auto-update PRs
- Auto-merges the new PR
The .golangci.yml enables most linters but disables:
gochecknoinits- Allowsinit()functionsexhaustruct- Zero-valued fields are acceptabletestpackage- Not doing black-box testingwrapcheck- Too noisyfunlen,cyclop,gocognit- Function complexity checks disabledgochecknoglobals- Global variables allowed for legitimate use cases
Special rules:
- Variable naming accepts both "Id" and "ID" (via revive)
- Short variable names allowed within 15 lines (via varnamelen)
- Tests use
github.com/stretchr/testifyfor assertions - Package
debugis for local debugging only and should not be imported in production code
Many packages expose Prometheus metrics:
- Actor: message counts, processing time, panics, queue depth
- Pool: object counts, creation/close events, errors
- Metrics use subsystem labels for multi-tenancy
If you encounter errors like Permission denied (publickey) or module download failures:
-
Verify your SSH key is added to GitHub and has access to amp-labs repositories
-
Test GitHub SSH connection:
ssh -T git@github.com
You should see:
Hi username! You've successfully authenticated... -
Verify Go environment:
go env GOPRIVATE # Should show: github.com/amp-labs/* -
Test module access:
go list -m github.com/amp-labs/amp-common
Problem: Module download fails with authentication errors
- Solution: Ensure
git config --global url."git@github.com:".insteadOf "https://github.com/"is set
Problem: IDE shows "module not found" errors
- Solution: Restart your IDE after setting environment variables, ensure SSH key is loaded
Problem: Tests fail to import private dependencies
- Solution: Verify
GOPRIVATEis set in your environment and SSH authentication is working
