Documentation ¶
Overview ¶
Package env env包提供环境变量的操作同时支持dotenv
使用NewEnvLoader()创建一个环境加载器实例 获取初始类型string的环境变量
e := NewEnvLoader() e.Get("KEY").Raw() 自动转换为Int类型 e.Get("KEY").Int()
使用dotenv来加载文件,默认会读取.env配置文件 env.Load() // 传入file1 file2
支持以开发模式的方式自动加载,根据环境变量GO_DEVMODE配置 env.AutoLoad()
Package env
Index ¶
- Constants
- func AutoLoad()
- func GetEnv(key string) string
- func GetEnvs(keys ...string) []string
- func IsEnvEqual(env, dst string) bool
- func Load(files ...string) error
- func Marshal(envs map[string]string) (string, error)
- func Overload(files ...string) error
- func Read() (map[string]string, error)
- func SetEnvContext(c context.Context, key, val string) context.Context
- func Unmarshal(s string) (map[string]string, error)
- func UnmarshalBytes(b []byte) (map[string]string, error)
- func Write(envs map[string]string, file string) error
- type EnvLoader
- func (e *EnvLoader) Bool() bool
- func (e *EnvLoader) Float32() float32
- func (e *EnvLoader) Float64() float64
- func (e *EnvLoader) Get(key string) *EnvLoader
- func (e *EnvLoader) GetValue(key string) string
- func (e *EnvLoader) Int() int
- func (e *EnvLoader) Int16() int16
- func (e *EnvLoader) Int32() int32
- func (e *EnvLoader) Int64() int64
- func (e *EnvLoader) Int8() int8
- func (e *EnvLoader) Interface() interface{}
- func (e *EnvLoader) MustBool(def bool) bool
- func (e *EnvLoader) MustFloat32(def float32) float32
- func (e *EnvLoader) MustFloat64(def float64) float64
- func (e *EnvLoader) MustInt(def int) int
- func (e *EnvLoader) MustInt16(def int16) int16
- func (e *EnvLoader) MustInt32(def int32) int32
- func (e *EnvLoader) MustInt64(def int64) int64
- func (e *EnvLoader) MustInt8(def int8) int8
- func (e *EnvLoader) MustString(def string) string
- func (e *EnvLoader) Raw() string
- func (e *EnvLoader) Time() time.Time
Constants ¶
View Source
const ( DEFAULT = ".env" DEV = "development" PROD = "production" DEVFILE = ".env.dev" PRODFILE = ".env.prod" GO_DEVMODE = "GO_DEVMODE" )
Variables ¶
This section is empty.
Functions ¶
func AutoLoad ¶ added in v1.1.10
func AutoLoad()
AutoLoad 默认读取当前目录下的.env 在不同开发模式时自动匹配 内置了dev prod模式分别会匹配.env.dev和.env.prod 根据当前环境变量GO_DEVMODE判断
func Read ¶ added in v1.1.10
Read 尝试从环境变量中读取合并的环境变量到Map var myEnv map[string]string myEnv, err := Read()
var1 := myEnv["Var1"]
func SetEnvContext ¶
SetEnvContext 设置上下文环境变量
Types ¶
type EnvLoader ¶ added in v1.0.4
type EnvLoader struct { KeepRawValue bool // 断言失败时返回原始值 为false时raw永远为原始值的空值 // contains filtered or unexported fields }
func (*EnvLoader) MustFloat32 ¶ added in v1.0.7
func (*EnvLoader) MustFloat64 ¶ added in v1.0.7
func (*EnvLoader) MustString ¶ added in v1.1.5
Click to show internal directories.
Click to hide internal directories.