go build -tags构建约束试验示例解析
作者:疯长的胡茬
使用方法
- 构建约束以一行
+build开始的注释。在+build之后列出了一些条件,在这些条件成立时,该文件应包含在编译的包中; - 约束可以出现在任何源文件中,不限于go文件;
 +build必须出现在package语句之前,+build注释之后应要有一个空行。
// +build debug
package main
import "fmt"
func main() {
 fmt.Println("Hello World!")
}语法规则
1)只允许是字母数字或_
2)多个条件之间,空格表示OR;逗号表示AND;叹号(!)表示NOT
3)一个文件可以有多个+build,它们之间的关系是AND。如:
// +build linux darwin // +build 386 等价于 // +build (linux OR darwin) AND 386
4)预定义了一些条件:
runtime.GOOS、runtime.GOARCH、compiler(gc或gccgo)、cgo、context.BuildTags中的其他单词
5)如果一个文件名(不含后缀),以 *_GOOS, *_GOARCH, 或 *_GOOS_GOARCH结尾,它们隐式包含了 构建约束
6)当不想编译某个文件时,可以加上// +build ignore。这里的ignore可以是其他单词,只是ignore更能让人知道什么意思
更多详细信息,可以查看go/build/build.go文件中shouldBuild和match方法。
应用实例 Build constraints来实现
除了*_GOOS这种预定义的应用,我们看一个实际的应用。
比如,项目中需要在测试环境输出Debug信息,一般通过一个变量(或常量)来控制是测试环境还是生产环境,比如:if DEBUG {},这样在生产环境每次也会进行这样的判断。在golang-nuts邮件列表中有人问过这样的问题,貌似没有讨论出更好的方法(想要跟C中条件编译一样的效果)。下面我们采用Build constraints来实现。
1)文件列表:main.go logger_debug.go logger_product.go
2)在main.go中简单的调用Debug()方法。
3)在logger_product.go中的Debug()是空实现,但是在文件开始加上// + build !debug
4)在logger_debug.go中的Debug()是需要输出的调试信息,同时在文件开始加上// + build debug
这样,在测试环境编译的时传递-tags参数:go build/install -tags “debug” logger。生产环境:go build/install logger就行了。
对于生产环境,不传递-tags时,为什么会编译logger_product.go呢?因为在go/build/build.go中的match方法中有这么一句:
if strings.HasPrefix(name, "!") { // negation
    return len(name) > 1 && !ctxt.match(name[1:])
}也就是说,只要有!(不能只是!),tag不在BuildTags中时,总是会编译。
应用实例 编译的tag差异
本例程中,编译的tag差异较大,两个文件中一个是hash tag,一个是int tag,需要引入第三个tag来区分编译的文件。否则,只要不带!的tag都会被编译进包。
display_hash.go
// +build hash !display_alternatives
// 上面
package main
import "fmt"
type DisplayName string
func Print(name DisplayName) {
    fmt.Printf("%s\n", name)
}
func MakeDisplayName(name string) DisplayName {
    return DisplayName(name)
}display_int.go
// +build int
package main
import (
    "fmt"
    "encoding/hex"
    "encoding/binary"
)
type DisplayName uint64
func Print(name DisplayName) {
    fmt.Printf("%d\n", name)
}
func MakeDisplayName(name string) DisplayName {
    h, err := hex.DecodeString(name)
    if err != nil {
        panic(fmt.Sprintf("decode hex string failed. cause: %v\n", err))
    }
    fmt.Printf("data: %v\n", h)
    value := binary.BigEndian.Uint16(h)
    return DisplayName(value)
}build_tag.go
package main
import (
    "sync"
    "fmt"
    "math/rand"
    "time"
)
func main() {
    var name DisplayName
    name = MakeDisplayName("FAD9C812")
    Print(name)编译display_int.go编译执行过程 go build -tags "display_alternatives int"
编译display_hash.go编译执行过程 go build -tags hash
以上就是go build -tags构建约束试验示例解析的详细内容,更多关于go build tags约束构建的资料请关注脚本之家其它相关文章!
