process.env.VAR在Go中的替代

  • 本文关键字:Go env VAR process go
  • 更新时间 :
  • 英文 :


如何访问在Go中提供给单个程序的环境变量?

在NodeJS中,我使用:

const HTTP_PORT = process.env.HTTP_PORT
$ HTTP_PORT=5000 node index.js

我想知道如何在Golang完成这项工作。

您可以使用os.Getenv()

func main() {
httpPort := os.Getenv("HTTP_PORT")
fmt.Printf("HTTP_PORT=%sn", httpPort)
}

而执行:

$ HTTP_PORT=5000 go run test.go
HTTP_PORT=5000

最新更新