在 GOLANG 中分离开发和生产环境
Separting the development and production environment in GOLANG
我是 GO 编程新手。我来自nodejs。在 nodejs 中很容易分离 dev 和 prod 模式。只需使用此代码即可。
if(process.env.NODE_ENV==="production"){
server.listen(prod.port);
}
else{
server.listen(dev.port);
}
我基本上也想在 GO 中使用这个约定。那么我怎么能分开我的开发和生产代码。
想要这个功能的原因是
to separate the port my server is listening in dev and prod
environment
如果有什么技术可以分离端口,也可以。
P.S: I am using VScode as my code editor. And go-iris
as a go server
framework
你可以使用os.Getenv
功能。
package main
import (
"fmt"
"os"
)
func getEnv() string{
return os.Getenv("APP_ENV")
}
您可以通过多种方式在 Go 中实现此目的,但所有这些方式都不会在代码本身中处理。
- 使用环境变量,例如
os.Getenv
- 解析配置文件,例如一些
.yaml
文件
- 使用标志,例如
package flag
我认为最常见的解决方案是使用标志,但它们的作用都是一样的。通常你在 main 方法中解析你的标志。
我是 GO 编程新手。我来自nodejs。在 nodejs 中很容易分离 dev 和 prod 模式。只需使用此代码即可。
if(process.env.NODE_ENV==="production"){
server.listen(prod.port);
}
else{
server.listen(dev.port);
}
我基本上也想在 GO 中使用这个约定。那么我怎么能分开我的开发和生产代码。
想要这个功能的原因是
to separate the port my server is listening in dev and prod environment
如果有什么技术可以分离端口,也可以。
P.S: I am using VScode as my code editor. And
go-iris
as a go server framework
你可以使用os.Getenv
功能。
package main
import (
"fmt"
"os"
)
func getEnv() string{
return os.Getenv("APP_ENV")
}
您可以通过多种方式在 Go 中实现此目的,但所有这些方式都不会在代码本身中处理。
- 使用环境变量,例如
os.Getenv
- 解析配置文件,例如一些
.yaml
文件 - 使用标志,例如
package flag
我认为最常见的解决方案是使用标志,但它们的作用都是一样的。通常你在 main 方法中解析你的标志。