功能简述
将系统的环境变量获取并解析到结构体的包,通过io 包的 Getenv 等方法获取系统设置环境变量的值,将这些值 赋值给结构体对应的字段,初始化结构体字段值
使用场景
用于构建项目的配置文件config
支持的类型和默认值 (官方文档+翻译内容)
Supported types and defaults 支持的类型和默认值
Out of the box all built-in types are supported, plus a few others that are commonly used.
开箱即用的所有内置类型都得到了支持,另外还有一些 是常用的。
Complete list:完整列表:
string
bool
int
int8
int16
int32
int64
uint
uint8
uint16
uint32
uint64
float32
float64
time.Duration
encoding.TextUnmarshaler
url.URL
Pointers, slices and slices of pointers, and maps of those types are also supported.
指针、指针的片和片以及那些类型的映射也是 支持。
You can also use/define a custom parser func for any other type you want.
你也可以使用/定义一个自定义的解析器函数 你想要的其他类型。
You can also use custom keys and values in your maps, as long as you provide a parser function for them.
您还可以在地图中使用自定义键和值,只要提供 parser函数为它们。
If you set the envDefault tag for something, this value will be used in the case of absence of it in the environment.
如果您为某个对象设置了envDefault标记,则此值将在 在环境中不存在的情况下。
By default, slice types will split the environment value on ,; you can change this behavior by setting the envSeparator tag.
默认情况下,切片类型将在,;你可以改变 通过设置envSeparator标记来实现此行为。
##代码演示
本人是windows 10 系统,代码中得os.Setenv 为模拟设置系统环境变量,为方便代码测试使用
package main
import (
"fmt"
"os"
"time"
"github.com/caarlos0/env/v9"
)
func init() {
os.Setenv("HOME", "myhome")
os.Setenv("PRODUCTION", "true")
os.Setenv("HOSTS", "host1:host2:host3")
os.Setenv("DURATION", "1s")
os.Setenv("TEMP_FOLDER", "tmp\\b.txt")
os.Setenv("TESTUNSET", "UNSET YES")
}
type config struct {
Home string `env:"HOME,required,expand"`
Port int `env:"PORT" envDefault:"3000"`
IsProduction bool `env:"PRODUCTION,notEmpty"`
Hosts []string `env:"HOSTS" envSeparator:":"`
Duration time.Duration `env:"DURATION"`
TempFolder string `env:"TEMP_FOLDER,file" envDefault:"${TEMP_FOLDER}"`
CachePath string `env:"CACHE_PATH,expand" envDefault:"${HOME}/cache"`
TestUnset string `env:"TESTUNSET"`
}
func main() {
//读取指定系统变量是否存在
show := func(key string) {
val, ok := os.LookupEnv(key)
if !ok {
fmt.Printf("%s not set\n", key)
} else {
fmt.Printf("%s=%s\n", key, val)
}
}
//读取env 配置的数据
cfg := config{}
if err := env.Parse(&cfg); err != nil {
fmt.Printf("%+v\n", err)
}
fmt.Printf("%+v\n", cfg)
//检查系统环境变量HOME是否设置
show("TESTUNSET")
}
使用 反单引号 env:“环境变量名称” env.Parse 解析环境变量的值,将这些值赋值给对应的结构体字段
Port int env:"PORT" envDefault:"3000"
envDefault 如果对应的系统环境变量未设置,增加 envDefault 配置 会为该项增加 默认配置值
打印结果
Port:3000
切片类型 设置 env:“环境变量名称” envSeparator:“分隔符号”`
envSeparator 配置 分隔符
Hosts []string env:"HOSTS" envSeparator:":"
上面代码中设置 struct 结构体字段 Hosts 是一个[]string 字符串切片类型,以: 做分割符号
模拟设置系统环境变量
os.Setenv("HOSTS", "host1:host2:host3")
打印结果
Hosts:[host1 host2 host3]
env tag 选项
evn 有一些标签属性这些标签属性可以同时使用
tag required (必需传入的)
Home string `env:"HOME,required,expand`
当没有设置HOME环境变量时会提示
env: required environment variable "HOME" is not set
tag notEmpty (不能为空)
IsProduction bool `env:"PRODUCTION,notEmpty"`
当没有设置PRODUCTION环境变量时会提示
env: environment variable "PRODUCTION" should not be empty
tag expand (扩展)
CachePath string `env:"CACHE_PATH,expand" envDefault:"${HOME}/cache"`
expand 设置后再 envDefault 中可以 通过 ${系统环境变量} 方式读取环境变量内容
os.Setenv("HOME", "myhome")
输出内容如下
CachePath:myhome/cache
tag file 读取文件内容
TempFolder string `env:"TEMP_FOLDER,file" envDefault:"${TEMP_FOLDER}"`
我设置的文件是
os.Setenv("TEMP_FOLDER", "tmp\\b.txt")
输出内容如下
TempFolder:this my file b
读取到 tmp\b.txt 内容 this my file b
tag unset
当设置 unset时 会删除 对应的环境变量,防止后面的文件读取该信息
当前未设置 unset 标签时,通过show 方法调用 读取系统环境变量可以读取到设置的环境变量
TESTUNSET=UNSET YES
设置unset 标签后
TestUnset string `env:"TESTUNSET,unset"`
输出内容
{Home:myhome Port:6000 IsProduction:true Hosts:[host1 host2 host3] Duration:1s CachePath:myhome/cache TempFolder:this my fil
e b TestUnset:UNSET YES}
TESTUNSET not set
系统环境变量TESTUNSET不能直接读取,已经被删掉,只能通过配置文件读取内容,