在Go语言中,可以使用ldflags来设置未分配的整数吗?

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

Is it possible in Go to use ldflags to set an unassigned integer?

问题

我知道有几种方法可以通过添加一点额外的代码来完成这个任务,但这让我很好奇...是否有一种方法可以使用ldflags在Go应用程序中设置一个未分配的整数?例如,你可以调用...

go build -ldflags "-X main.CurrentEnvironment 1"

然后它会设置为:

package main

var CurrentEnvironment int

func main() {

    ...

}

我没有找到任何支持这一点的文档,所以我认为答案是否定的,但也许有一种方法。

英文:

I know there are several ways of accomplishing this with just a little extra code, but it got me curious... Is there a way to set an unassigned integer in a Go application using ldflags? For instance, could you call...

go build -ldflags "-X main.CurrentEnvironment 1"

And have it set:

package main

var CurrentEnvironment int

func main() {

    ...

}

I couldn't find any documentation supporting this so I presume the answer is no but maybe there's a way.

答案1

得分: 5

你可以使用-X标志来设置一个字符串值(文档)。然后你可以在主函数中使用Atoi将其转换为整数。

英文:

You can use the -X flag to set a string value only (docs). You could then convert it to an int in your main function with Atoi.

huangapple
  • 本文由 发表于 2015年9月6日 00:34:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/32415348.html
匿名

发表评论

匿名网友

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

确定