Golang有条件编译 [英] Golang conditional compilation

查看:180
本文介绍了Golang有条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Go 1的条件编译中遇到了麻烦。



这是我的测试代码。有什么我误解了// +构建约束和-tags标志?

main1.go

  // + build main1 
package main

import(
fmt


func main(){
fmt.Println(This is main 1)
}

main2.go

  // + build main2 
package main

进口(
fmt


func main(){
fmt.Println(This is main 2)
}

运行go build时,我仍然有编译错误

  $ go build -tags'main1'
#test
./main2.go:8:在此块中重新声明
先前的声明在./main1.go:8


解决方案

您必须按照 // +构建XXX ,并留空行。在我简短的搜索中, t找到/如果这是记录。但来源清楚地说出来了

I've got a trouble with conditional compilation in Go 1.

Here is my test code. Is there anything I misunderstand about the "// +build" constraint and the "-tags" flag?

main1.go

// +build main1
package main

import (
    "fmt"
)

func main() {
    fmt.Println("This is main 1")
}

main2.go

// +build main2
package main

import (
    "fmt"
)

func main() {
    fmt.Println("This is main 2")
}

when running "go build", I still got compile error

$ go build -tags 'main1'
# test
./main2.go:8: main redeclared in this block
        previous declaration at ./main1.go:8

解决方案

You must follow // +build XXX with a blank line.

In my brief search, I couldn't find where/if this is documented. But the source clearly calls it out

这篇关于Golang有条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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