组织环境变量Golang

Jor*_*ero 12 environment-variables go

在Node.js中,我使用nconf模块为每个项目提供环境变量,如S3键,GCM键等.

我无法在Go中找到类似的解决方案.

有哪些普遍接受的工具可以帮助管理每个Go项目的环境变量?

提前致谢.

Pet*_*son 26

我强烈建议改用github.com/namsral/flag.这就像内置的标志除了可以通过环境变量提供参数.

例如,假设您有以下代码:

package main

import "fmt"
import "github.com/namsral/flag"

func main() {
    var port = 3000
    flag.IntVar(&port, "port", port, "Port number")
    flag.Parse()
    fmt.Println("You seem to prefer", port)
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用命令行选项或环境变量提供值:

:~/dev/GO$ go run dummy.go
You seem to prefer 3000
:~/dev/GO$ go run dummy.go -port=1234
You seem to prefer 1234
:~/dev/GO$ PORT=4321 go run dummy.go
You seem to prefer 4321
:~/dev/GO$ PORT=4321 go run dummy.go -port=5555
You seem to prefer 5555
Run Code Online (Sandbox Code Playgroud)

当提供命令行参数很难时,这可能很重要.例如,如果您使用gin自动重新启动服务器,则无法提供命令行参数,因为gin它只是调用go run主代码而不传递任何参数.


edu*_*911 11

当我开始使用Go时,我曾经对此做了一些阅读.根据这个链接http://peter.bourgon.org/go-in-production/,他们建议使用CLI标志(参数)而不是环境变量 - 他们甚至将环境变量转换为其应用程序的标志.

花了一些时间来适应; 但是,我确实看到了在开发,登台和生产环境之间使用纯CLI标志的优势 - 为每个环境提供特定的脚本.

例如,这是我最近写的一个小网页应用程序:

// global flags
var isdebug bool
var port int
var cert string
var key string
var dbdsn string
var dbmaxidle int
var dbmaxopen int
var imguri string

// init is the entry point for the entire web application.
func init() {

    log.Println("Starting wwwgo ...")

    // setup the flags
    //flag.StringVar(&host, "host", "", "Specify a host to redirect to. Use this to redirect all traffic to a single url.")
    flag.IntVar(&port, "port", 8080, "Specify the port to listen to.")
    flag.BoolVar(&isdebug, "isdebug", false, "Set to true to run the app in debug mode.  In debug, it may panic on some errors.")
    flag.StringVar(&cert, "cert", "", "Enables listening on 443 with -cert and -key files specified.  This must be a full path to the certificate .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.")
    flag.StringVar(&key, "key", "", "Enables listening on 443 with -cert and -key files specified.  This must be a full path to the key .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.")
    flag.StringVar(&dbdsn, "dbdsn", "root:root@tcp(localhost:3306)/dev_db?timeout=5s&tls=false&autocommit=true", "Specifies the MySql DSN connection.")
    flag.IntVar(&dbmaxidle, "dbmaxidle", 0, "Sets the database/sql MaxIdleConns.")
    flag.IntVar(&dbmaxopen, "dbmaxopen", 500, "Sets the database/sql MaxOpenConns.")
    flag.StringVar(&imguri, "imguri", "/cdn/uploads/", "Set this to the full base uri of all images, for example on a remote CDN server or local relative virtual directory.")
    flag.Parse()

    // log our flags
    if isdebug != false {
        log.Println("DEBUG mode enabled")
    }
    if cert != "" && key != "" {
        log.Println("Attempting SSL binding with supplied cert and key.")
    }
    if dbdsn != "" {
        log.Printf("Using default dbdsn: %s", dbdsn)
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

这在临时/生产环境中真的变得很好.

一个快速./wwwgo -h"那个参数到底是什么?" 为您提供完整的文档:

admin@dev01:~/code/frontend/src/wwwgo [master]$ ./wwwgo -h
Usage of ./wwwgo:
  -cert="": Enables listening on 443 with -cert and -key files specified.  This must be a full path to the certificate .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.
  -dbdsn="root:root@tcp(localhost:3306)/dev_db?timeout=5s&tls=false&autocommit=true": Specifies the MySql DSN connection.
  -dbmaxidle=0: Sets the database/sql MaxIdleConns.
  -dbmaxopen=500: Sets the database/sql MaxOpenConns.
  -imguri="/cdn/uploads/": Set this to the full base uri of all images, for example on a remote CDN server or local relative virtual directory.
  -isdebug=false: Set to true to run the app in debug mode.  In debug, it may panic on some errors.
  -key="": Enables listening on 443 with -cert and -key files specified.  This must be a full path to the key .pem file. See http://golang.org/pkg/net/http/#ListenAndServeTLS for more information.
  -port=8080: Specify the port to listen to.
Run Code Online (Sandbox Code Playgroud)

很高兴在CLI上有很多选项,并且不需要文档 - 它内置在flags包中.

您可以立即清楚地看到默认值.

有了这种类型的文档,我倾向于为团队使用的常见"开发环境"设置所有默认值.我们都有对本地数据库的root/root访问权限.我们都在开发期间使用端口8080用于此特定Web应用程序等.这样,您只需运行:

go build
./wwwgo
Run Code Online (Sandbox Code Playgroud)

该应用程序运行所有默认值 - 记录的默认值.在生产中,只需覆盖默认值.如果任何参数格式错误,内置标志解析器将使应用程序发生混乱,这也非常好.