如何正确使用构建标签? [英] How to properly use build tags?

查看:73
本文介绍了如何正确使用构建标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够构建go应用程序的不同版本;

I need to be able to build different versions of a go application; a 'debug' version and a normal version.

这很容易做到;我只是有一个const DEBUG,它可以控制应用程序的行为,但是每次我需要在构建类型之间进行交换时,都不得不编辑配置文件很烦人。

This is easy to do; I simply have a const DEBUG, that controls the behaviour of the application, but it's annoying to have to edit the config file every time I need to swap between build types.

我正在阅读有关go build( http://golang.org/pkg/go/build/)和标签,我想也许我可以这样做:

I was reading about go build (http://golang.org/pkg/go/build/) and tags, I thought perhaps I could do this:

config.go:

config.go:

// +build !debug
package build
const DEBUG = false

config.debug.go:

config.debug.go:

// +build debug
package build
const DEBUG = true

然后我应该可以使用 go build或 go build-标签调试,并且标签应排除config.go并包含config.debug.go。

Then I should be able to build using "go build" or "go build -tags debug", and the tags should exclude config.go and include config.debug.go.

...但这无法正常工作。我得到:

...but this doesn't work. I get:


src / build / config.go:3:在此块中重新调试(< 0>)前$ b $在src / build / config.debug.go:3上进行b声明:3

src/build/config.go:3: DEBUG redeclared in this block (<0>) previous declaration at src/build/config.debug.go:3

我在做什么错了?

我应该使用其他更合适的#ifdef样式吗?

Is there another and more appropriate #ifdef style way of doing this I should be using?

推荐答案

请参见我对另一个问题的回答。您需要在 // + build行之后留空行。

See my answer to another question. You need a blank line after the "// +build" line.

此外,您可能想要!在config.go中,而不是在config.debug.go中;并且可能您希望其中一个是 DEBUG = false。

Also, you probably want the "!" in config.go, not in config.debug.go; and presumably you want one to be "DEBUG = false".

这篇关于如何正确使用构建标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆