惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Stack Overflow Blog
Stack Overflow Blog
Forbes - Security
Forbes - Security
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
N
News | PayPal Newsroom
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Project Zero
Project Zero
Cyberwarzone
Cyberwarzone
Vercel News
Vercel News
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Google Online Security Blog
Google Online Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
T
Tenable Blog
I
InfoQ
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
D
DataBreaches.Net
博客园 - Franky
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
S
Security @ Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
J
Java Code Geeks
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Full Disclosure
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Cloudflare Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs
Jina AI
Jina AI
V
Visual Studio Blog
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
After 5 years of Go services, here's the boilerplate I wish existed
rock288 · 2026-05-17 · via DEV Community

TL;DR: I open-sourced rock288/go-mongo-boilerplate — a Go 1.25 service template that ships the boring production stuff (observability, retry, DLQ, SSRF-safe HTTP, health splits, graceful shutdown) so you don't write it for the 7th time. Click "Use this template" and start with make scaffold name=Order.


The problem with every Go boilerplate I've used

I've started six Go services in the last five years. The first 200 lines were always the same:

  • Wire up a slog handler that auto-injects trace_id
  • Pick a config library (koanf? viper? envconfig?) and write a Validate()
  • Add Mongo + an event.CommandMonitor that doesn't leak memory
  • Build a Kafka consumer with retry topic + DLQ
  • Hand-roll graceful shutdown for two consumer goroutines
  • Realize the HTTP client is wide-open to SSRF and patch the dialer
  • Split /healthz (liveness) from /readyz (readiness)
  • Spend an afternoon making golangci-lint, mockery, and Wire all happy together

Most Go boilerplates on GitHub solve one of these. None of the ones I found solved all of them in a way that felt opinionated rather than kitchen-sink-y.

So I wrote one. Then I wrote it again. Then I extracted the third iteration into a public template.

This post is a tour of the parts that took me the most rewrites to get right — and the trade-offs I'd argue with you about.


1. Package-by-feature + a tiny platform/ layer

The most common boilerplate mistake: models/, controllers/, services/ directories. That layout scales to maybe four features before you grep for User and get 14 files in 7 packages.

internal/
  user/                 ← one feature, all in one place
    model.go
    repository.go
    service.go
    handler.go
    dto.go
    routes.go
    errors.go
    wire.go
  role/                 ← another feature
  platform/             ← cross-feature infrastructure
    config/  logger/  database/  httpserver/  httpclient/
    kafka/   sqs/      health/   observability/  resilience/
    pagination/         httperror/

Enter fullscreen mode Exit fullscreen mode

The hard rule: features call each other only through exported interfaces (UserService, not userService). Cross-feature dependencies on concrete types are a code smell. Lint rule on this would be nice — open to suggestions.

The internal/platform/ layer is everything that doesn't belong to a single feature. Any feature may import platform; features may not import each other's internal symbols.

Adding a new feature is one command:

make scaffold name=Order

Enter fullscreen mode Exit fullscreen mode

A bash script clones internal/user/, runs perl to rename identifiers, and prints the wire-up checklist. From make scaffold to a working GET /orders endpoint is ~10 minutes.


2. MongoDB v2 driver — and why you probably haven't migrated yet

The go.mongodb.org/mongo-driver/v2 GA'd, but most templates on GitHub are still on v1. The v2 driver has a cleaner cursor API, generics support, and — crucially for production — there's still no official OTel instrumentation package.

So I wrote a custom event.CommandMonitor:

// internal/platform/database/mongo_otel.go
client, err := mongo.Connect(options.Client().
    ApplyURI(uri).
    SetMonitor(NewCommandMonitor(tracer)))

Enter fullscreen mode Exit fullscreen mode

The monitor keeps a RequestID → span map populated on CommandStarted and consumed on CommandSucceeded / CommandFailed. The catch: if a command never completes (network drop, server restart) the entry leaks. So there's a janitor goroutine that purges entries older than 2 minutes.

// Sketch — full impl in the repo
type CommandMonitor struct {
    inflight sync.Map  // RequestID → spanEntry
}

func (m *CommandMonitor) Started(ctx, evt) {
    _, span := tracer.Start(ctx, "mongo."+evt.CommandName)
    m.inflight.Store(evt.RequestID, spanEntry{span, time.Now()})
}

func (m *CommandMonitor) Succeeded(ctx, evt) {
    if v, ok := m.inflight.LoadAndDelete(evt.RequestID); ok {
        v.span.End()
    }
}

Enter fullscreen mode Exit fullscreen mode

Hot take: every project I've seen that uses MongoDB in Go has at least one production memory leak in their tracing layer. Mine probably did too until I wrote that janitor.


3. Kafka retry + DLQ that actually works under load

This is the part of the template I've rewritten the most times across jobs.

Rules I now live by:

  1. Three topics per logical event: events, events.retry, events.dlq. No magic, no in-memory queues.
  2. Retry topic has its own consumer group (<group>-retry). When it polls a message, it sleeps min(base * 2^n, max) before republishing to the main topic. This separates retry latency from main-topic throughput.
  3. x-retry-count and x-error-reason are headers, not body fields. The body is opaque to the platform.
  4. Inbound x-retry-count and x-error-reason are stripped on republish. Attacker can't pre-set retry count to force DLQ. (Spotted on a real pentest. Yes, it was me.)
  5. Idempotency key auto-generated on publish if not provided. Consumers dedupe via this key, not message offset.
// The contract the platform exposes
type MessageHandler interface {
    Handle(ctx context.Context, record *kgo.Record) error
}

// Return:
//   nil                                  → commit offset (ACK)
//   errors.Is(err, context.Canceled)     → no commit, no republish (shutdown)
//   errors.Is(err, kafka.ErrNonRetryable) → straight to DLQ
//   any other error                      → retry topic with incremented count

Enter fullscreen mode Exit fullscreen mode

W3C traceparent is injected on the producer side and extracted on the consumer side so distributed traces don't break when crossing the broker.

producer.Publish(ctx, "user-events", key, body)
//                  ↑
//                  span context auto-propagated into the record header

Enter fullscreen mode Exit fullscreen mode

The failure path calls ForceSample() so retry/DLQ traffic always shows up on the dashboard — sampling shouldn't hide errors.


4. SQS as a second broker — same API, same routing

Plenty of teams pick SQS over Kafka when they're AWS-only and don't need replay history. So the template ships both, side-by-side, with deliberately identical routing semantics:

// Kafka
type kafka.MessageHandler interface { Handle(ctx, *kgo.Record) error }

// SQS — same shape
type sqs.MessageHandler interface { Handle(ctx, *types.Message) error }

// Same return contract:
//   nil → ACK
//   ctx.Canceled → drop
//   ErrNonRetryable → DLQ
//   other err → retry queue with backoff

Enter fullscreen mode Exit fullscreen mode

The same make scaffold walkthrough works for both. The cmd/worker binary runs four goroutines via errgroup.WithContext (Kafka main+retry, SQS main+retry); first non-nil cancels the shared ctx and all consumers shut down together.

Gotchas the docs warn you about:

  • SQS MessageDelaySeconds caps at 900s. Backoff > 15 min silently clamps. (For longer backoffs you need an external scheduler.)
  • MessageAttributes limit is 10 keys; 3 are reserved (traceparent, tracestate, x-idempotency-key). Producer rejects with ErrTooManyAttributes if the caller exceeds the budget.
  • Standard SQS does NOT guarantee ordering — don't expect Kafka-partition semantics.

LocalStack for dev:

make localstack-up
make sqs-create-queues   # provisions events / events-retry / events-dlq + RedrivePolicy

Enter fullscreen mode Exit fullscreen mode

Production: queues are provisioned by your IaC (Terraform/CDK). The worker only needs sqs:Send/Receive/Delete/GetQueueUrl/ChangeMessageVisibility — no :CreateQueue. Boilerplate ships infra code only; you wire your own sqs.MessageHandler.


5. Compile-time DI with google/wire — once you try it you can't go back

Runtime DI containers feel clever until you spend two hours debugging "nil pointer" because container key resolution failed at startup.

google/wire generates the wiring at compile time. If a provider is missing, the build fails. There's no reflection, no runtime overhead, and the generated file is grep-able.

//go:build wireinject

func InitializeServer(cfgPath string) (*ServerApp, func(), error) {
    wire.Build(
        config.Load,
        ProvideMongoConfig,
        database.NewClient,
        user.ProviderSet,           // one line wires the whole feature
        role.ProviderSet,
        ProvideRouter,
        wire.Struct(new(ServerApp), "*"),
    )
    return nil, nil, nil
}

Enter fullscreen mode Exit fullscreen mode

make wire regenerates wire_gen.go. CI fails on drift — so a contributor adding a feature without running make wire gets blocked.

The trade-off: there's a learning curve. But after the first feature, every subsequent addition follows the same ProviderSet pattern and Claude Code (or any agent) can copy it.

Which brings me to —


6. The vibe-coding angle: CLAUDE.md + AGENTS.md

If you've used Claude Code, Cursor, Aider, or Windsurf agent mode, you know they all read the repo for context. Most repos give them nothing useful — agent ends up cargo-culting whatever pattern it finds first.

I committed two files specifically for AI agents:

  • CLAUDE.md (215 lines) — commands, architecture, coding conventions, package boundaries, "Adding a feature" checklist
  • AGENTS.md — multi-agent compat redirect (Cursor and Aider both read this convention)
## Coding Conventions

**Naming**
- Interfaces: no `I` prefix. `UserService` (interface) / `userService` (impl)
- Constructors `NewUserService` return the interface
- Errors: sentinel `ErrXxx` in feature-local `errors.go`
...

## Adding an SQS-consuming feature

1. Create `internal/<feature>/event_handler_sqs.go` implementing `sqs.MessageHandler`
2. Replace `ProvideSQSHandler` in `cmd/worker/providers.go`
3. Add `<feature>.NewSQSEventHandler` to `cmd/worker/wire.go` ProviderSet
4. `make wire mocks test`

Enter fullscreen mode Exit fullscreen mode

The payoff is real: I can paste a Linear ticket into Claude Code, and it produces a 80%-correct PR because it knows the repo's conventions, the test patterns, and which file to edit. The agent doesn't waste context discovering — the discovery is one file.

This is a controversial design choice. Some people don't want their repo "locked into" Claude. But CLAUDE.md is just markdown — re-readable by humans, ignorable by anyone who doesn't use AI. Cost: 215 lines. Benefit: agents move from "best-guess" to "follows your conventions".


7. The boring stuff you wish someone had written for you

Things in the template you'd otherwise reinvent on day 4 of your project:

  • /healthz (liveness) vs /readyz (readiness) with cached checkers and fail-after-N hysteresis so a transient blip doesn't flap your load balancer.
  • SSRF-safe HTTP client (httpclient.NewExternalClient) — custom Dialer rejects loopback, RFC1918, link-local, and AWS metadata IPs (169.254.169.254). The default for any non-hardcoded URL.
  • http.Server timeouts wired correctly (ReadHeaderTimeout, ReadTimeout, WriteTimeout, IdleTimeout) — these CANNOT be replaced by handler-level timeouts because body read happens before the handler.
  • Middleware ordering documented with rationale:
  otelgin → Recovery → RequestID → RequestLogger → RateLimit → BodyLimit → Timeout → CORS → handler

Enter fullscreen mode Exit fullscreen mode

  • Cursor pagination as a platform-level helper — opaque base64(JSON) cursor, generic Page[T] envelope, ClampLimit capped at 100.
  • Shared error envelope {error: {code, message}} via httperror.BadRequest(c, err) so handlers don't redefine gin.H{"error": err.Error()} 47 times.
  • X-Request-ID propagated through ctx, response headers, and slog fields — debug correlation from client log → server trace.
  • Distroless Docker with healthcheck via the binary's healthcheck subcommand (because distroless has no curl/wget).
  • make ci runs lint + test + race + govulncheck + gitleaks. The CI workflow blocks merges on any of these.

None of these are revolutionary. Together they save you the first two sprints.


What's NOT in the template (and why)

I'm allergic to "kitchen sink" boilerplates. Things deliberately excluded:

  • Auth / AuthZ — every team has different requirements (session vs JWT, OPA vs Casbin vs hand-rolled). Adding any choice is a wrong choice for 80% of users.
  • Integration tests with testcontainers — go-kit philosophy: build the integration harness when you have a real feature that needs it, not on speculation.
  • CQRS / event sourcing — demand-driven. If your domain needs it, you know.
  • API versioning gymnastics (v1/, v2/) — the boilerplate has /api/v1 baked in; multi-version is the user's call.
  • GraphQL — different concern.
  • Frontend — wrong repo.

If you need any of these, the template is a starting point, not a finished product.


How to use it

# 1. Click "Use this template" on GitHub, or:
git clone https://github.com/rock288/go-mongo-boilerplate my-service
cd my-service

# 2. Rename the module path (everywhere it appears)
NEW=github.com/<your-org>/<your-repo>
OLD=github.com/rock288/go-mongo-boilerplate
go mod edit -module=$NEW
grep -rl "$OLD" --include='*.go' --include='*.yml' --include='*.yaml' --include='Makefile' --include='*.md' . \
  | xargs perl -pi -e "s|$OLD|$NEW|g"
make wire && make mocks

# 3. Start your first feature
make scaffold name=Order
# → internal/order/ ready; edit model.go, dto.go, wire it up

# 4. Run
docker-compose up -d
make migrate-up
make run                 # http://localhost:8002

Enter fullscreen mode Exit fullscreen mode

make ci should be green. If it isn't, that's a bug — open an issue.


What I'd like feedback on

  • Is internal/middleware/ cleanly separated, or should it move under internal/platform/httpserver/middleware/?
  • The SQS app-level retry queue costs an extra SendMessage per failure compared to native SQS redrive. I chose it for Kafka-symmetry. Worth the cost?
  • MessageDelaySeconds 900s cap forces clamping. For backoffs >15min I think the right answer is "use Step Functions" — but it's not built in. Should it be?
  • CLAUDE.md + AGENTS.md — pro or con for a public template? I've heard both.

Open an issue if you have opinions or — better — a PR.


Links

If this saved you an afternoon, leave a star or share with a teammate who's about to start their seventh Go service.


If you spot a bug, an outdated dep, or a convention I should fight you about, open an issue. The repo gets better when people argue.