fx for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is this library and why is it needed

\n\n

fx is a powerful dependency injection (DI) framework for the Go language, developed by Uber. It provides an elegant way to manage dependencies in Go applications by automating the creation, binding, and lifecycle of objects.

\n\n

In a traditional Go application, developers often face the problem of "constructor hell," where creating one object requires passing many dependencies. This leads to:

\n
    \n
  • Bulky code with manual dependency management
  • \n
  • Difficulties in testing
  • \n
  • Challenges in changing the application architecture
  • \n
\n\n

fx solves these problems by providing:

\n
    \n
  • Automatic dependency injection — the framework itself determines which objects are needed and in what order to create them
  • \n
  • Lifecycle management — automatic startup and shutdown of components
  • \n
  • Modularity — the application is broken down into independent modules
  • \n
  • Testability — easily replace real dependencies with mock objects
  • \n
\n\n

fx is especially useful when developing:

\n
    \n
  • Microservices
  • \n
  • Web applications
  • \n
  • CLI tools
  • \n
  • Any applications with a complex dependency architecture
  • \n
\n\n

Installation

\n\n

Installing fx is done using the standard method for Go libraries via go get:

\n\n
go get go.uber.org/fx
\n\n

To update to the latest version:

\n\n
go get -u go.uber.org/fx
\n\n

After installation, import the library into your code:

\n\n
import "go.uber.org/fx"
\n\n

It is also recommended to install the fx utility for code generation (optional):

\n\n
go install go.uber.org/fx/cmd/fx@latest
\n\n

Quick start with code example

\n\n

Let's look at a simple example of using fx to create an HTTP server:

\n\n
package main\n\nimport (\n    "context"\n    "fmt"\n    "net/http"\n    "go.uber.org/fx"\n)\n\n// NewHandler creates an HTTP handler\nfunc NewHandler() http.Handler {\n    mux := http.NewServeMux()\n    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {\n        fmt.Fprintf(w, "Hello, fx!")\n    })\n    return mux\n}\n\n// NewServer creates an HTTP server\nfunc NewServer(handler http.Handler) *http.Server {\n    return &http.Server{\n        Addr:    ":8080",\n        Handler: handler,\n    }\n}\n\n// StartServer starts the server\nfunc StartServer(lc fx.Lifecycle, server *http.Server) {\n    lc.Append(fx.Hook{\n        OnStart: func(ctx context.Context) error {\n            go server.ListenAndServe()\n            fmt.Println("Server started on :8080")\n            return nil\n        },\n        OnStop: func(ctx context.Context) error {\n            return server.Shutdown(ctx)\n        },\n    })\n}\n\nfunc main() {\n    app := fx.New(\n        fx.Provide(NewHandler),\n        fx.Provide(NewServer),\n        fx.Invoke(StartServer),\n    )\n    app.Run()\n}
\n\n

This example demonstrates the basic mechanics of fx:

\n
    \n
  • fx.Provide — registers constructors
  • \n
  • fx.Invoke — runs functions that use dependencies
  • \n
  • fx.Lifecycle — manages the lifecycle of components
  • \n
\n\n

Main methods/functions

\n\n

1. fx.New

\n

What it does: Creates a new fx application that manages dependencies. This is the main entry point into the framework.

\n

Parameters:

\n
    \n
  • options ...Option — a variadic function that accepts one or more parameters of type Option (fx.Provide, fx.Invoke, fx.Module, etc.)
  • \n
\n

Working code example:

\n
packag                    

Recommendations