Golang中与Node的NODE_ENV等效的是什么?

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

What is the Golang equivalent of Node's NODE_ENV

问题

我正在尝试设置模板化,根据环境变量来指示我们处于开发、暂存还是生产环境,渲染的HTML将指向不同的服务器。

为了做到这一点,我想使用类似Node中的NODE_ENV环境变量的方式,并根据此在我的代码中有条件地设置路径。

if(process.env.NODE_ENV === 'development') {
  /* 开发环境代码 */
} else {
  /* 生产环境代码 */
}

在Golang中是否有定义环境的标准方法?这种方法是否合理?

英文:

I'm trying to set up templating whereby based on an environment variable to indicate whether we are in a development, staging or production environment, the HTML that is rendered is pointed towards different servers.

To do this I would like to use something like Node does in the NODE_ENV environment variable, and then conditionally set the path based on that in my code.

if(process.env.NODE_ENV === 'development') {
  /* development code */
} else {
  /* production code */
}

Is there a standard for defining an environment in Golang? Does this seem like a logical approach?

答案1

得分: 6

是的,这是一种合乎逻辑的方法。实际上,在部署服务器时,这是一种相当标准的做法。

在Go语言中没有标准,所以可以随意选择。

只需尽量保持明显,例如:APP_ENV=dev

英文:

Yes, it is a logical approach. In fact it's a somewhat standard practice when deploying servers.

There is no standard in the go world, so do whatever.

Just try to keep it obvious, ie: APP_ENV=dev

答案2

得分: 2

在Go语言中似乎没有NODE_ENV的等效项。您可以在本地的IDE(以及生产环境)中设置一个名为APP_ENV的环境变量。

env := os.Getenv("APP_ENV")
if env == "production" {
  /* 生产环境下的代码 */
} else {
  /* 开发环境下的代码 */
}

请注意,以上代码是用于根据环境变量选择不同代码版本的示例。您可以根据自己的需求进行相应的修改。

英文:

There doesn't appear to an equivalent of NODE_ENV in the Go world. You can set an environment variable APP_ENV in your IDE locally (and in production).

env := os.Getenv("APP_ENV")
if env == "production" {
  /* production-version of the code */
} else {
  /* development-version of the code */
}

答案3

得分: -2

你仍然可以使用"NODE_ENV",它只是一个名称,然后使用os.Getenv("NODE_ENV")获取它。

英文:

you can still using "NODE_ENV", it's just a name, then use os.Getenv("NODE_ENV")get it.

huangapple
  • 本文由 发表于 2017年2月19日 06:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/42321201.html
匿名

发表评论

匿名网友

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

确定