美文网首页
Go Wire Basic

Go Wire Basic

作者: 戈壁堂 | 来源:发表于2020-08-07 17:54 被阅读0次

    go wire

    前提:已经将$GOPATH/bin目录添加到环境变量$PATH
    安装go get github.com/google/wire/cmd/wire

    wire tutorial

    • Goland 提示 wire.go doesn't match to garget system. File will be ignored by build tool

    在设置--> Go --> Build Tags & Vendoring中的Custom tags里指定要用的编译tag,例如 wireinject。参考指定编译tag编译go build -tags编译官方手册

    • 执行wire命令时提示go list stderr <<go: finding module for package pattern=. >>
    go list stderr <<go: finding module for package pattern=. >>
    wire: packages not found
    wire: generate failed
    

    官方issue #125中的解决方案——

    # 先升级 go/packages,可以添加 -v参数看到升级了哪些packages
    go get -u golang.org/x/tools/go/packages
    # 再重新安装 wire
    go get github.com/google/wire/cmd/wire
    

    解释是这样的——

    So Wire uses golang.org/x/tools/go/packages behind the scenes in order to gather all of the files to parse. go/packages is specifically designed to be agnostic to source code layout (e.g. modules versus GOPATH workspaces), so it shells out to the go tool to obtain the information that it needs. Since this requires cooperation from both your installed go tool and the go/packages version, an outdated go/packages library can mess up the communication and report bad results to Wire (which is what happened here).

    In the Go modules future, your built version of Wire would have used a known-tested version of go/packages and this issue would not have occurred.

    执行完之后,可以正常生成wire_gen.go文件。可以先执行wire check检查是否符合编译条件

    执行完之后,可以正常生成wire_gen.go文件。可以先执行wire check检查是否符合编译条件。

    执行完一次wire命令之后,再次需要更新wire_gen.go文件时,执行go generate命令即可

    undefined: InitializeEvent, 使用wire生成注入代码之后,编译时需要带上对应的代码。

    执行 go build默认使用了上次的配置? go build main.go wire_gen.go可生成可执行文件

    相关文章

      网友评论

          本文标题:Go Wire Basic

          本文链接:https://www.haomeiwen.com/subject/tthqdktx.html