使用uber-go的fx进行依赖注入

入门 下面是一个官方的例子: package main import ( "context" "fmt" "go.uber.org/fx" "io" "net" "net/http" "os" ) func main() { fx.New( fx.Provide( NewHTTPServer, NewEchoHandler, NewServeMux, ), fx.Invoke(func(srv *http.Server) {}), ).Run() } func NewHTTPServer(lc fx.Lifecycle, mux *http.ServeMux) *http.Server { srv := &http.Server{ Addr: ":8080", Handler: mux, } lc.Append(fx.Hook{ OnStart: func(ctx context.Context) error { ln, err := net.Listen("tcp", srv.Addr) if err != nil { return err } fmt.Println("Starting HTTP server at", srv.Addr) go srv.Serve(ln) return nil }, OnStop: func(ctx context.Context) error { return srv.Shutdown(ctx) }, }) return srv } // EchoHandler is an http.Handler that copies its request body // back to the response. type EchoHandler struct{} // NewEchoHandler builds a new EchoHandler. func NewEchoHandler() *EchoHandler { return &EchoHandler{} } // ServeHTTP handles an HTTP request to the /echo endpoint. func (*EchoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if _, err := io.Copy(w, r.Body); err != nil { fmt.Fprintln(os.Stderr, "Failed to handle request:", err) } } // NewServeMux builds a ServeMux that will route requests // to the given EchoHandler. func NewServeMux(echo *EchoHandler) *http.ServeMux { mux := http.NewServeMux() mux.Handle("/echo", echo) return mux } 详解 fx.Lifecycle 使用 fx.Lifecycle 对象向应用程序添加生命周期挂钩。这告诉 Fx 如何启动和停止 HTTP 服务器。 ...

2023-12-13 · 7 分钟 · czyt