登录
首页 >  Golang >  Go问答

Uber Fx - 调用

来源:stackoverflow

时间:2024-04-26 20:39:33 275浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《Uber Fx - 调用》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

如何让以下功能正常工作?

对于 lifecycle 函数,我需要传递 foo 的 2 个不同实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
 
import (
    "context"
    "fmt"
    "go.uber.org/fx"
)
 
type foo interface {
    print()
}
 
type bar struct{}
 
func (b *bar) print() {
    fmt.println("i'm bar")
}
 
type baz struct{}
 
func (b *baz) print() {
    fmt.println("i'm baz")
}
 
func foo() foo {
    return &bar{}
}
 
func anotherfoo() foo {
    return &baz{}
}
 
func main() {
    workingstart() //this works
    //nonworkingstart() //this does not
}
 
func nonworkingstart() {
 
    app := fx.new(
        fx.provide(fx.annotate(foo, fx.resulttags(`name:"bar"`))),
        fx.provide(fx.annotate(anotherfoo, fx.resulttags(`name:"baz"`))),
        //how to configure nonworkingrun having 2 different foo's in its arguments?
        fx.invoke(nonworkingrun),
    )
 
    if app.err() != nil {
        panic(fmt.errorf("unable to bootstrap app: %w", app.err()))
    }
 
    app.run()
}
 
func workingstart() {
 
    app := fx.new(
        fx.provide(foo),
        fx.invoke(workingrun),
    )
 
    if app.err() != nil {
        panic(fmt.errorf("unable to bootstrap app: %w", app.err()))
    }
 
    app.run()
}
 
func nonworkingrun(lifecycle fx.lifecycle, foo foo, anotherfoo foo) {
    lifecycle.append(
        fx.hook{
            onstart: func(context.context) error {
                foo.print()
                anotherfoo.print()
                return nil
            },
        },
    )
}
 
func workingrun(lifecycle fx.lifecycle, foo foo) {
    lifecycle.append(
        fx.hook{
            onstart: func(context.context) error {
                foo.print()
                return nil
            },
        },
    )
}

我通过以下方式实现了它。不确定除了使用保存 foo 切片的结构并使用 fx.paramtags 构建该结构之外是否还有其他方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
type FooSlices struct {
    fooSlices []Foo
}
 
func fooSlices(foo, anotherFoo Foo) FooSlices {
    return FooSlices{fooSlices: []Foo{foo, anotherFoo}}
}
 
func main() {
    //workingStart()
    nonWorkingStart()
}
 
func nonWorkingStart() {
 
    app := fx.New(
        fx.Provide(fx.Annotate(foo, fx.ResultTags(`name:"bar"`))),
        fx.Provide(fx.Annotate(anotherFoo, fx.ResultTags(`name:"baz"`))),
        fx.Provide(fx.Annotate(fooSlices, fx.ParamTags(`name:"bar"`, `name:"baz"`))),
        fx.Invoke(nonWorkingRun),
    )
 
    if app.Err() != nil {
        panic(fmt.Errorf("unable to bootstrap app: %w", app.Err()))
    }
 
    app.Run()
}
 
 
func nonWorkingRun(lifecycle fx.Lifecycle, fooSlices FooSlices) {
    lifecycle.Append(
        fx.Hook{
            OnStart: func(context.Context) error {
                for _, foo := range fooSlices.fooSlices {
                    foo.Print()
                }
                return nil
            },
        },
    )
}

正确答案


收集一部分输入相当难以推理,也不太可扩展,并且违背了 fx 的“意图”(隐藏样板代码)的精神。

首先进行少量重构

  • fx.provide 可以接受一系列输入,只需调用一次
  • n foo 实现仅需要 n-1 注释,因为第一个注释可以是原始类型,其余的是 annotated 类型
  • 避免恐慌,如果发生就让它自然发生;这意味着存在语法问题,可以在推送代码之前缓解该问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fx.New(
    fx.Provide(
        foo,
        fx.Annotate(foo, fx.ResultTags(`name:"baz"`)),
    ),
    fx.Invoke(toBeInvoked),
).Run()
 
type Params struct {
    fx.In
 
    Lifecycle fx.Lifecycle
    Bar foo
    Baz foo `name:"baz"`
}
 
func toBeInvoked(p Params) {
    p.Lifecycle.Append(
        fx.Hook{
            OnStart: func(_ context.Context) error {
                p.Bar.Print()
                p.Baz.Print()
                return nil
            },
        },
    )
}

在 fx 中还有其他方法可以做到这一点,这是一种相当常见的模式。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Uber Fx - 调用》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>