A collection of foundational Go primitives for building declarative, struct-tag-driven libraries.
This SDK provides shared building blocks used across my ecosystem, including:
- Struct Tag Parsing: Generic
key:valuetag parser - Dependency Injection: Minimal, type-safe DI container
- Adapter Pattern: Pluggable backend registry
- Hook Discovery: Automatic lifecycle hook detection
go get github.com/mirkobrombin/go-foundationGeneric parser for struct tags with key:value syntax.
import "github.com/mirkobrombin/go-foundation/pkg/tags"
p := tags.NewParser("guard", tags.WithPairDelimiter(";"))
result := p.Parse("role:owner; read:admin,user")
// result["role"] = ["owner"]
// result["read"] = ["admin", "user"]Minimal DI container with generics support.
import "github.com/mirkobrombin/go-foundation/pkg/di"
c := di.New()
c.Provide("db", myDB)
db := di.Get[*sql.DB](c, "db")Generic registry for swappable adapters/backends.
import "github.com/mirkobrombin/go-foundation/pkg/adapters"
r := adapters.NewRegistry[Transport]()
r.Register("http", httpTransport)
r.Register("grpc", grpcTransport)
r.SetDefault("http")
t := r.Default()Automatic discovery of lifecycle methods via reflection.
import "github.com/mirkobrombin/go-foundation/pkg/hooks"
d := hooks.NewDiscovery()
methods := d.Discover(myStruct, "OnEnter")
// Returns map of "OnEnterPaid", "OnEnterCancelled", etc.Generic functional options pattern.
import "github.com/mirkobrombin/go-foundation/pkg/options"
type Config struct { Host string; Port int }
func WithHost(h string) func(*Config) { return func(c *Config) { c.Host = h } }
cfg := &Config{}
options.Apply(cfg, WithHost("localhost"))Generic concurrent map with helpers.
import "github.com/mirkobrombin/go-foundation/pkg/safemap"
m := safemap.New[string, int]()
m.Set("count", 1)
m.Compute("count", func(v int, _ bool) int { return v + 1 })Functional error handling.
import "github.com/mirkobrombin/go-foundation/pkg/result"
r := result.Try(func() (int, error) { return strconv.Atoi("123") })
doubled := result.Map(r, func(n int) int { return n * 2 })
fmt.Println(doubled.UnwrapOr(0)) // 246Circuit Breaker and Retry with exponential backoff.
import "github.com/mirkobrombin/go-foundation/pkg/resiliency"
cb := resiliency.NewCircuitBreaker(3, time.Minute)
err := cb.Execute(func() error { return doWork() })
err := resiliency.Retry(ctx, func() error { return doWork() }, resiliency.WithAttempts(5))Common interfaces for distributed or local locking.
import "github.com/mirkobrombin/go-foundation/pkg/lock"
// Use with your Redis/Etcd locker implementation
func Process(l lock.Locker) {
l.Acquire(ctx, "resource-1", time.Second)
defer l.Release(ctx, "resource-1")
}Thread-safe generic collections like Set.
import "github.com/mirkobrombin/go-foundation/pkg/collections"
s := collections.NewSet[string]()
s.Add("item-1", "item-2")
if s.Has("item-1") { ... }Aggregation and grouping of multiple errors.
import "github.com/mirkobrombin/go-foundation/pkg/errors"
errs := &errors.MultiError{}
errs.Append(err1, err2)
return errs.ErrorOrNil()Universal string-to-type binder.
import "github.com/mirkobrombin/go-foundation/pkg/reflect"
var count int
reflect.Bind(reflect.ValueOf(&count).Elem(), "42")This library consolidates patterns that were duplicated across multiple of my projects, I just thought it would be a good idea to have a shared library for these primitives.
go-struct-flags is now deprecated. Replace:
// Before
import "github.com/mirkobrombin/go-struct-flags/v2/pkg/binder"
// After
import "github.com/mirkobrombin/go-foundation/pkg/tags"as for now, they are fully compatible and can be used interchangeably.
MIT License. See LICENSE for details.