我可以传递构建命令标志并在代码中读取吗?

huangapple go评论85阅读模式
英文:

Can I pass to build command flag and read inside code?

问题

我想在init函数中执行以下两行代码之一:

func init() {
    log.SetPrefix(">>>>>>>>>>   ")
    log.SetOutput(ioutil.Discard)
}

如何实现类似于C语言中的宏定义和makefile中的宏定义?我使用go build my_project来构建Go程序,没有自定义的makefile。我可以在构建命令中传递标志,并在代码中读取吗?

英文:

I want in init function to execute one of those two lines

func init() {
	log.SetPrefix(">>>>>>>>>>>>>   ")


	log.SetOutput(ioutil.Discard)
}

How to achieve something similiar to C and macro definition in make file ?
I build go program like go build my_project and I don't have any custom make file. Can I pass to build command flag and read inside code ?

答案1

得分: 6

创建一个字符串变量,你可以进行测试。然后使用传递给链接器的-X参数设置该字符串变量。例如:

package main

var MODE string

func main() {
    if MODE == "PROD" {
        println("I'm in prod mode")
    } else {
        println("I'm NOT in prod mode")
    }
}

现在,如果你只使用go build进行构建,MODE将是""。但你也可以这样构建:

go build -ldflags "-X main.MODE PROD"

现在,MODE将是"PROD"。你可以根据构建设置修改逻辑。我通常使用这种技术在二进制文件中设置版本号,但它也可以用于各种构建时配置。请注意,它只能设置字符串。因此,你不能将MODE设置为布尔值或整数。


你还可以使用标签来实现这一点,这需要设置稍微复杂一些,但更加强大。创建三个文件:

main.go

package main

func main() {
    doThing()
}

main_prod.go

// +build prod

package main

func doThing() {
    println("I'm in prod mode")
}

main_devel.go

// +build !prod

package main

func doThing() {
    println("I'm NOT in prod mode")
}

现在,当你使用go build进行构建时,你将得到main_devel.go版本(文件名无关紧要,只要顶部的// +build !prod行)。但是,你可以通过使用go build -tags prod进行构建来获得生产版本。

请参阅build文档中有关构建约束的部分。

英文:

Create a string variable that you can test. Then set that string variable using -X passed to the linker. For example:

package main

var MODE string

func main() {
	if MODE == "PROD" {
		println("I'm in prod mode")
	} else {
		println("I'm NOT in prod mode")
	}

}

Now, if you build this with just go build, MODE will be "". But you can also build it this way:

go build -ldflags "-X main.MODE PROD"

And now MODE will be "PROD". You can use that to modify your logic based on build settings. I generally use this technique to set version numbers in the binary; but it can be used for all kinds of build-time configuration. Note that it can only set strings. So you couldn't make MODE a bool or integer.


You can also achieve this with tags, which are slightly more complicated to set up, but much more powerful. Create three files:

main.go

package main

func main() {
	doThing()
}

main_prod.go

// +build prod

package main

func doThing() {
	println("I'm in prod mode")
}

main_devel.go

// +build !prod

package main

func doThing() {
	println("I'm NOT in prod mode")
}

Now when you build with go build, you'll get your main_devel.go version (the filename doesn't matter; just the // +build !prod line at the top). But you can get a production build by building with go build -tags prod.

See the section on Build Constraints in the build documentation.

huangapple
  • 本文由 发表于 2015年5月25日 03:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/30427289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定