envconfig.Process() 做什么
What does envconfig.Process() do
我正在使用 the envconfig library 查看一些源代码,但无法理解以下代码的作用。我知道它会加载环境变量,但想了解每一行的作用。我希望有人能向我解释一下。特别是 envconfig.Process("", &Env)
行的作用
package config
import (
"html/template"
"log"
"os"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
type envVars struct {
Dbhost string `required:"true" envconfig:"DB_HOST"`
Dbport string `required:"true" envconfig:"DB_PORT"`
Dbuser string `required:"true" envconfig:"DB_USER"`
Dbpassword string `required:"true" envconfig:"DB_PASS"`
Dbname string `required:"true" envconfig:"DB_NAME"`
JwtKey string `required:"true" envconfig:"JWT_KEY"`
HashKey string `required:"true" envconfig:"HASH_KEY"`
}
//Env holds application config variables
var Env envVars
// Tpl template
var Tpl *template.Template
func init() {
wd, err := os.Getwd() //get path of working directory(current directory) - directory of this project
if err != nil {
log.Println(err, "::Unable to get paths")
}
Tpl = template.Must(template.ParseGlob(wd + "/internal/views/*.html")) //could use path.join in case it's used on linux instead of windows.
//load .env file
err = godotenv.Load(wd + "/./.env") //loads environment variable file so that env variables can be accessed in code eg. by using os.GetEnv("DB_DIALECT"), won't work otherwise.
if err != nil {
log.Println("Error loading .env file, falling back to cli passed env")
}
err = envconfig.Process("", &Env)
if err != nil {
log.Fatalln("Error loading environment variables", err)
}
}
envconfig.Process() 使用从环境变量中提取的值填充给定结构。可以使用 envconfig
struct 标记指定使用哪些环境变量。
例如:
Dbhost string `required:"true" envconfig:"DB_HOST"`
以上将使用 DB_HOST
环境变量的值填充 Dbhost
字段。如果 required
标签设置为 true,如果不存在匹配的环境变量,Process
将 return 出错。
如果要为不存在匹配环境变量的情况定义默认值,可以使用 default
标签:
Dbhost string `default:"host1" envconfig:"DB_HOST"`
Process
的第一个参数是一个前缀,以便只匹配具有特定前缀的环境变量。
例如:
envconfig.Process("DB", &env)
以上仅考虑带有 DB_
前缀的环境变量,例如DB_HOST
、DB_PORT
、DB_USER
等。在您的特定情况下,这将使字段 JwtKey
和 HashKey
未填充,因为相应的环境变量不会有一个 DB_
前缀。
我建议查看 README documentation on Github,其中提供了一些更详细的解释和示例。
我正在使用 the envconfig library 查看一些源代码,但无法理解以下代码的作用。我知道它会加载环境变量,但想了解每一行的作用。我希望有人能向我解释一下。特别是 envconfig.Process("", &Env)
行的作用
package config
import (
"html/template"
"log"
"os"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
type envVars struct {
Dbhost string `required:"true" envconfig:"DB_HOST"`
Dbport string `required:"true" envconfig:"DB_PORT"`
Dbuser string `required:"true" envconfig:"DB_USER"`
Dbpassword string `required:"true" envconfig:"DB_PASS"`
Dbname string `required:"true" envconfig:"DB_NAME"`
JwtKey string `required:"true" envconfig:"JWT_KEY"`
HashKey string `required:"true" envconfig:"HASH_KEY"`
}
//Env holds application config variables
var Env envVars
// Tpl template
var Tpl *template.Template
func init() {
wd, err := os.Getwd() //get path of working directory(current directory) - directory of this project
if err != nil {
log.Println(err, "::Unable to get paths")
}
Tpl = template.Must(template.ParseGlob(wd + "/internal/views/*.html")) //could use path.join in case it's used on linux instead of windows.
//load .env file
err = godotenv.Load(wd + "/./.env") //loads environment variable file so that env variables can be accessed in code eg. by using os.GetEnv("DB_DIALECT"), won't work otherwise.
if err != nil {
log.Println("Error loading .env file, falling back to cli passed env")
}
err = envconfig.Process("", &Env)
if err != nil {
log.Fatalln("Error loading environment variables", err)
}
}
envconfig.Process() 使用从环境变量中提取的值填充给定结构。可以使用 envconfig
struct 标记指定使用哪些环境变量。
例如:
Dbhost string `required:"true" envconfig:"DB_HOST"`
以上将使用 DB_HOST
环境变量的值填充 Dbhost
字段。如果 required
标签设置为 true,如果不存在匹配的环境变量,Process
将 return 出错。
如果要为不存在匹配环境变量的情况定义默认值,可以使用 default
标签:
Dbhost string `default:"host1" envconfig:"DB_HOST"`
Process
的第一个参数是一个前缀,以便只匹配具有特定前缀的环境变量。
例如:
envconfig.Process("DB", &env)
以上仅考虑带有 DB_
前缀的环境变量,例如DB_HOST
、DB_PORT
、DB_USER
等。在您的特定情况下,这将使字段 JwtKey
和 HashKey
未填充,因为相应的环境变量不会有一个 DB_
前缀。
我建议查看 README documentation on Github,其中提供了一些更详细的解释和示例。