core-go

The core of our Go services.

core-go is the shared foundation we build Go services on: configuration, HTTP and Lambda entry points, events over Kafka and SQS, caching, rate limiting, state machines and a set of small typed helpers.

go get github.com/pixie-sh/core-go

Go 1.25+. MIT licensed. The package map is in the README.

The problem

Every new service rebuilds the same plumbing.

Loading configuration, starting a server, handling shutdown, emitting events, caching, logging with a trace ID. None of it is the product, and all of it has to be right.

Start from a working base

A service implements one interface and gets bootstrap, health, metrics and graceful shutdown.

Take only what you need

Each package is imported on its own. Nothing forces the rest on you.

The same pieces everywhere

Errors, logs, IDs and configuration look alike in every service, so moving between them is easy.

What is inside

Building blocks in pkg, connected components in infra.

pkg has no infrastructure behind it. infra talks to Redis, Kafka, SQS or the database.

AreaPackagesWhat you get
Servicepkg/microservice, pkg/configuration, pkg/contextbootstrap through di-go, config from JSON or TOML with environment placeholders, logger and trace ID in the context
HTTPpkg/comm/http, pkg/comm/http_middlewares, pkg/comm/restserver, request parsing, logging, metrics, tracing and CORS middlewares, an outbound REST client
AWS Lambdapkg/lambda and subpackagesentry points for API Gateway, SQS and Kafka, with routing
Eventsinfra/events, infra/message_router, pkg/pubsubtyped events, Kafka and SQS producers and consumers, routing and broadcasting
Stateinfra/cache, infra/rate_limiter, infra/state_machineRedis cache and locks, rate limiting, state machines with conditions and history
Operationsinfra/cron, infra/database_migrator, pkg/metricsscheduled jobs, migrations as a service, tracing and metrics
Helperspkg/types, pkg/uid, pkg/files, pkg/templatesgeneric slices, sets and maps, scoped IDs, file and image handling, templates
Usage

A few of the pieces.

import "github.com/pixie-sh/core-go/pkg/configuration"

type Config struct {
    Port     int `json:"port"`
    Database struct {
        DSN string `json:"dsn"`
    } `json:"database"`
}

var cfg Config
_, err := configuration.StructFromJSONBytesWithEnvReplace(
    []byte(`{"port": 8080, "database": {"dsn": "${env.DB_DSN}"}}`),
    &cfg,
    logger.Logger,
)
// with DB_DSN=postgres://app@db:5432/app
// cfg.Database.DSN == "postgres://app@db:5432/app"
import "github.com/pixie-sh/core-go/pkg/uid"

const ScopeInvoice uid.Scope = 7

id := uid.NewScoped(ScopeInvoice)

fmt.Println(id.String()) // 26-character, time-sortable
scope, _ := id.Scope()   // 7

same, err := uid.FromString(id.String())
import (
    "github.com/pixie-sh/core-go/pkg/types/sets"
    "github.com/pixie-sh/core-go/pkg/types/slices"
)

names := slices.Map([]int{1, 2, 2, 3}, func(i int) string {
    return fmt.Sprintf("user-%d", i)
})

slices.Deduplicate(names)        // [user-1 user-2 user-3]
slices.Contains(names, "user-3") // true

seen := sets.From([]string{"a", "b", "a"}) // 2 items
_, ok := sets.Contains(seen, "b")          // true
import "github.com/pixie-sh/core-go/infra/events"

event := events.NewEventWrapper("evt-1", "invoice.paid", InvoicePaid{InvoiceID: id})

// events.Emit and events.EmitBatch send through the default producers.
// Kafka and SQS implementations live in infra/events/kafka and infra/events/sqs.
Open source at Pixie

One of a set that works together.

These libraries run in our own services every day. Use one, or all of them.