英文:
What does envconfig.Process() do
问题
我正在使用envconfig库查看一些源代码,并且对下面的代码理解有困难。我知道它加载环境变量,但我想了解每一行具体的作用。我希望有人能够解释给我听。特别是这一行代码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() //获取工作目录(当前目录)的路径-此项目的目录
if err != nil {
log.Println(err, "::Unable to get paths")
}
Tpl = template.Must(template.ParseGlob(wd + "/internal/views/*.html")) //在Linux上使用path.join而不是Windows上的路径。
//加载.env文件
err = godotenv.Load(wd + "/./.env") //加载环境变量文件,以便可以在代码中访问环境变量,例如使用os.GetEnv("DB_DIALECT"),否则无法工作。
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)
}
}
英文:
I'm looking through some source code using the envconfig library and am having trouble understanding what the below code does. I know it loads the environment variables but would like to understand what each specific line does. I was hoping somebody might be able to explain it to me. In particular what the line envconfig.Process("", &Env)
does
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)
}
}
答案1
得分: 1
envconfig.Process() 函数会使用环境变量的值填充给定的结构体。可以使用 envconfig
结构标签来指定使用哪些环境变量。
例如:
Dbhost string `required:"true" envconfig:"DB_HOST"`
上述代码会将 Dbhost
字段填充为 DB_HOST
环境变量的值。如果 required
标签设置为 true,如果没有匹配的环境变量,Process
函数会返回一个错误。
如果你想为没有匹配的环境变量定义默认值,可以使用 default
标签:
Dbhost string `default:"host1" envconfig:"DB_HOST"`
Process
函数的第一个参数是一个前缀,用于只匹配具有特定前缀的环境变量。
例如:
envconfig.Process("DB", &env)
上述代码只会考虑以 DB_
为前缀的环境变量,比如 DB_HOST
、DB_PORT
、DB_USER
等。在你的特定情况下,这将导致 JwtKey
和 HashKey
字段未填充,因为相应的环境变量没有 DB_
前缀。
我建议你查阅 Github 上的 README 文档,其中提供了更详细的解释和示例。
英文:
envconfig.Process() populates a given struct with values pulled from environment variables. Which environment variables are used can be specified with the envconfig
struct tag.
For example:
Dbhost string `required:"true" envconfig:"DB_HOST"`
The above will populate the Dbhost
field with the value of the DB_HOST
environment variable. If the required
tag is set to true, Process
will return an error if no matching environment variable exists.
You can use the default
tag if you want to define defaults for the case where a matching environment variable does not exist:
Dbhost string `default:"host1" envconfig:"DB_HOST"`
The first parameter to Process
is a prefix in order to only match environment variables with a certain prefix.
For example:
envconfig.Process("DB", &env)
The above will only consider environment variables with a DB_
prefix, e.g. DB_HOST
, DB_PORT
, DB_USER
, etc. In your particular case, this would leave the fields JwtKey
and HashKey
unpopulated as the corresponding environment variables don't have a DB_
prefix.
I would suggest reviewing the README documentation on Github which provides some more detailed explanation and examples.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论