golang-uber-fx

Wire Go applications with uber-go/fx dependency injection, lifecycle hooks, and modules.

Updated Jun 15, 2026
One-click install
npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-uber-fx-2877389577
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-uber-fx
Source: https://github.com/2877389577/novels_ai_gen/tree/main/.agents/skills/golang-uber-fx
Command: npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-uber-fx-2877389577

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Building long-running Go services requires coordinating dependency wiring, startup ordering, graceful shutdown, and signal handling. This Skill guides AI agents to structure applications with uber-go/fx correctly, avoiding common mistakes like blocking OnStart hooks, init()-based side effects, and flat provider lists in main(). ## Core Features & Use Cases - Application Wiring: Use fx.New, fx.Provide, fx.Invoke, fx.Supply, and fx.Annotate (name/group/As tags) to build the dependency graph at the composition root. - Lifecycle & Modules: Register fx.Lifecycle OnStart/OnStop hooks, organize providers into fx.Module units with scoped decorators, and run signal-aware shutdown via app.Run() or manual Start/Stop. - Testing Patterns: Apply fxtest.New, fx.Populate, and fx.Replace to pull services from the graph and swap real dependencies for fakes without rewriting modules. - Use Case: When a Go service's main.go accumulates dozens of fx.Provide calls for HTTP, database, and metrics concerns, use this Skill to reorganize them into cohesive modules with value groups for route registration and graceful shutdown hooks. ## Quick Start Ask the agent to wire your Go service with uber-go/fx, for example: "Refactor my main.go to use fx.Module for the HTTP and database layers with lifecycle hooks for graceful shutdown."

Frequently Asked Questions about golang-uber-fx

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I start an HTTP server with uber-go/fx lifecycle hooks?▼

Inject fx.Lifecycle into your server constructor and append an fx.Hook. In OnStart, call net.Listen and launch srv.Serve in a goroutine so the hook returns quickly; in OnStop, call srv.Shutdown(ctx) for graceful shutdown.

What is the difference between uber-go/fx and uber-go/dig?▼

fx is built on dig and shares the same DI primitives, but adds lifecycle hooks, a module system with scoped decorators, a signal-aware run loop, and structured event logging. Use fx for long-running services and raw dig for CLI tools or libraries that manage their own lifecycle.

How do I replace a dependency with a fake in fx tests?▼

Use fxtest.New with fx.Replace to override a previously-provided type, even one buried inside a module, without rewriting the module. Combine with fx.Populate to extract the service under test and app.RequireStart/RequireStop for lifecycle management.

Why does my fx application hang during startup?▼

A blocking OnStart hook is the usual cause — long-running work must run in a goroutine so the hook returns quickly and dependent hooks can fire. Also verify every provided type is reachable from an fx.Invoke, since unreferenced constructors never run.

When should I use fx.Supply instead of fx.Provide?▼

Use fx.Supply for pre-built values like parsed config, secrets, or command-line flags that already exist before fx.New runs. It avoids writing a no-op constructor and makes the intent clearer than fx.Provide(func() *Config { return cfg }).

When should I avoid uber-go/fx for a Go project?▼

Avoid fx for one-shot CLI tools or small object graphs where lifecycle hooks and signal handling add no value. Use raw uber-go/dig for wiring without a framework, or manual constructor injection when the graph is very small.