当前位置 : 主页 > 大数据 > 区块链 >

go tags

来源:互联网 收集:自由互联 发布时间:2021-06-22
BuildTags smallnest edited this pageon 6 Feb 2018· 3 revisions 为了避免引入不必要的库, rpcx采用了 Go 条件编译 的特性, 你可以只引入必要的特性。 比如你只使用 etcd 作为注册中心的时候, 你不

BuildTags

  smallnest edited this page on 6 Feb 2018 ·  3 revisions

为了避免引入不必要的库, rpcx采用了 Go 条件编译 的特性, 你可以只引入必要的特性。 比如你只使用 etcd 作为注册中心的时候, 你不希望引入 consul、zookeeper相关的库,你需要在 Go BuildTags中指定 etcd。

如果不指定 tags,可能会出现下面的错误。

.server.go:37:8: undefined: serverplugin.ConsulRegisterPlugin

Build Tags

build tag 也叫做 Build Constraints, Go文档中已经详细介绍了,不熟悉的同学应该先阅读一下官方文档,学习相关的基础知识。

Go 的工具文档中介绍了 tags参数:

-tags ‘tag list‘ a space-separated list of build tags to consider satisfied during the build. For more information about build tags, see the description of build constraints in the documentation for the go/build package.

也就是在使用下列命名的时候

go buildgo clean go getgo installgo listgo rungo test

加上 -tags "tag1 tag2 tag3" 参数。

比如:

go build -tags "reuseport quic kcp zookeeper etcd consul ping" .

rpcx支持的tags

  • reuseport
  • quic
  • kcp
  • zookeeper
  • etcd
  • consul
  • ping

IDE

主流的IDE都支持buildtags参数的设置,比如

  • goland
  • vscode
  • vim-go
  • sublime-go
  • atom

你需要把 -tags "reuseport quic kcp zookeeper etcd consul ping" 加入到IDE的配置中,否则会出现IDE无法识别对应的文件,即时文件真实存在,IDE也不识别这个文件。

IDE设置buildtags文件方式各不相同,你应该查找对应的IDE的设置方法进行设置。

网友评论