{"id":42864,"date":"2024-12-01T10:48:54","date_gmt":"2024-12-01T02:48:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42864\/"},"modified":"2024-12-01T10:48:54","modified_gmt":"2024-12-01T02:48:54","slug":"uber-fx-%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42864\/","title":{"rendered":"Uber Fx &#8211; \u8c03\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Uber Fx &#8211; \u8c03\u7528<\/span><\/p>\n<h1>Uber Fx &#8211; \u8c03\u7528<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 20:39:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aUber Fx &#8211; \u8c03\u7528\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5982\u4f55\u8ba9\u4ee5\u4e0b\u529f\u80fd\u6b63\u5e38\u5de5\u4f5c\uff1f<\/p>\n<p>\u5bf9\u4e8e <code>lifecycle<\/code> \u51fd\u6570\uff0c\u6211\u9700\u8981\u4f20\u9012 <code>foo<\/code> \u7684 2 \u4e2a\u4e0d\u540c\u5b9e\u73b0\u3002<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"go.uber.org\/fx\"\n)\n\ntype foo interface {\n    print()\n}\n\ntype bar struct{}\n\nfunc (b *bar) print() {\n    fmt.println(\"i'm bar\")\n}\n\ntype baz struct{}\n\nfunc (b *baz) print() {\n    fmt.println(\"i'm baz\")\n}\n\nfunc foo() foo {\n    return &amp;bar{}\n}\n\nfunc anotherfoo() foo {\n    return &amp;baz{}\n}\n\nfunc main() {\n    workingstart() \/\/this works\n    \/\/nonworkingstart() \/\/this does not\n}\n\nfunc nonworkingstart() {\n\n    app := fx.new(\n        fx.provide(fx.annotate(foo, fx.resulttags(`name:\"bar\"`))),\n        fx.provide(fx.annotate(anotherfoo, fx.resulttags(`name:\"baz\"`))),\n        \/\/how to configure nonworkingrun having 2 different foo's in its arguments?\n        fx.invoke(nonworkingrun),\n    )\n\n    if app.err() != nil {\n        panic(fmt.errorf(\"unable to bootstrap app: %w\", app.err()))\n    }\n\n    app.run()\n}\n\nfunc workingstart() {\n\n    app := fx.new(\n        fx.provide(foo),\n        fx.invoke(workingrun),\n    )\n\n    if app.err() != nil {\n        panic(fmt.errorf(\"unable to bootstrap app: %w\", app.err()))\n    }\n\n    app.run()\n}\n\nfunc nonworkingrun(lifecycle fx.lifecycle, foo foo, anotherfoo foo) {\n    lifecycle.append(\n        fx.hook{\n            onstart: func(context.context) error {\n                foo.print()\n                anotherfoo.print()\n                return nil\n            },\n        },\n    )\n}\n\nfunc workingrun(lifecycle fx.lifecycle, foo foo) {\n    lifecycle.append(\n        fx.hook{\n            onstart: func(context.context) error {\n                foo.print()\n                return nil\n            },\n        },\n    )\n}<\/pre>\n<p>\u6211\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\u4e86\u5b83\u3002\u4e0d\u786e\u5b9a\u9664\u4e86\u4f7f\u7528\u4fdd\u5b58 foo \u5207\u7247\u7684\u7ed3\u6784\u5e76\u4f7f\u7528 <code>fx.paramtags<\/code> \u6784\u5efa\u8be5\u7ed3\u6784\u4e4b\u5916\u662f\u5426\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5<\/p>\n<pre>type FooSlices struct {\n    fooSlices []Foo\n}\n\nfunc fooSlices(foo, anotherFoo Foo) FooSlices {\n    return FooSlices{fooSlices: []Foo{foo, anotherFoo}}\n}\n\nfunc main() {\n    \/\/workingStart()\n    nonWorkingStart()\n}\n\nfunc nonWorkingStart() {\n\n    app := fx.New(\n        fx.Provide(fx.Annotate(foo, fx.ResultTags(`name:\"bar\"`))),\n        fx.Provide(fx.Annotate(anotherFoo, fx.ResultTags(`name:\"baz\"`))),\n        fx.Provide(fx.Annotate(fooSlices, fx.ParamTags(`name:\"bar\"`, `name:\"baz\"`))),\n        fx.Invoke(nonWorkingRun),\n    )\n\n    if app.Err() != nil {\n        panic(fmt.Errorf(\"unable to bootstrap app: %w\", app.Err()))\n    }\n\n    app.Run()\n}\n\n\nfunc nonWorkingRun(lifecycle fx.Lifecycle, fooSlices FooSlices) {\n    lifecycle.Append(\n        fx.Hook{\n            OnStart: func(context.Context) error {\n                for _, foo := range fooSlices.fooSlices {\n                    foo.Print()\n                }\n                return nil\n            },\n        },\n    )\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6536\u96c6\u4e00\u90e8\u5206\u8f93\u5165\u76f8\u5f53\u96be\u4ee5\u63a8\u7406\uff0c\u4e5f\u4e0d\u592a\u53ef\u6269\u5c55\uff0c\u5e76\u4e14\u8fdd\u80cc\u4e86 <code>fx<\/code> \u7684\u201c\u610f\u56fe\u201d\uff08\u9690\u85cf\u6837\u677f\u4ee3\u7801\uff09\u7684\u7cbe\u795e\u3002<\/p>\n<p>\u9996\u5148\u8fdb\u884c\u5c11\u91cf\u91cd\u6784<\/p>\n<ul>\n<li><code>fx.provide<\/code> \u53ef\u4ee5\u63a5\u53d7\u4e00\u7cfb\u5217\u8f93\u5165\uff0c\u53ea\u9700\u8c03\u7528\u4e00\u6b21<\/li>\n<li><code>n<\/code> <code>foo<\/code> \u5b9e\u73b0\u4ec5\u9700\u8981 <code>n-1<\/code> \u6ce8\u91ca\uff0c\u56e0\u4e3a\u7b2c\u4e00\u4e2a\u6ce8\u91ca\u53ef\u4ee5\u662f\u539f\u59cb\u7c7b\u578b\uff0c\u5176\u4f59\u7684\u662f <code>annotated<\/code> \u7c7b\u578b<\/li>\n<li>\u907f\u514d\u6050\u614c\uff0c\u5982\u679c\u53d1\u751f\u5c31\u8ba9\u5b83\u81ea\u7136\u53d1\u751f\uff1b\u8fd9\u610f\u5473\u7740\u5b58\u5728\u8bed\u6cd5\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u63a8\u9001\u4ee3\u7801\u4e4b\u524d\u7f13\u89e3\u8be5\u95ee\u9898<\/li>\n<\/ul>\n<pre>fx.New(\n    fx.Provide(\n        foo,\n        fx.Annotate(foo, fx.ResultTags(`name:\"baz\"`)),\n    ),\n    fx.Invoke(toBeInvoked),\n).Run()\n\ntype Params struct {\n    fx.In\n\n    Lifecycle fx.Lifecycle\n    Bar foo\n    Baz foo `name:\"baz\"`\n}\n\nfunc toBeInvoked(p Params) {\n    p.Lifecycle.Append(\n        fx.Hook{\n            OnStart: func(_ context.Context) error {\n                p.Bar.Print()\n                p.Baz.Print()\n                return nil\n            },\n        },\n    )\n}<\/pre>\n<p>\u5728 fx \u4e2d\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u8fd9\u662f\u4e00\u79cd\u76f8\u5f53\u5e38\u89c1\u7684\u6a21\u5f0f\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aUber Fx &#8211; \u8c03\u7528\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Uber Fx &#8211; \u8c03\u7528 Uber Fx &#8211; \u8c03\u7528 \u6765\u6e90\uff1astackoverflow 2024-04-26 20:39:33 0\u6d4f\u89c8 \u6536\u85cf \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aUber Fx &#8211; \u8c03\u7528\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u95ee\u9898\u5185\u5bb9 \u5982\u4f55\u8ba9\u4ee5\u4e0b\u529f\u80fd\u6b63\u5e38\u5de5\u4f5c\uff1f \u5bf9\u4e8e lifecycle \u51fd\u6570\uff0c\u6211\u9700\u8981\u4f20\u9012 foo \u7684 2 \u4e2a\u4e0d\u540c\u5b9e\u73b0\u3002 package main import ( &#8220;context&#8221; &#8220;fmt&#8221; &#8220;go.uber.org\/fx&#8221; ) type foo interface { print() } type bar struct{} func (b *bar) print() { fmt.println(&#8220;i&#8217;m bar&#8221;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42864","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42864"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42864\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}