Documentation ¶
Index ¶
- Variables
- func Exec(filenames []string, cmd string, cmdArgs []string) error
- func Load(filenames ...string) (err error)
- func Marshal(envMap map[string]string) (string, error)
- func Overload(filenames ...string) (err error)
- func Parse(r io.Reader) (envMap map[string]string, err error)
- func ParseConfig(v interface{}, opts ...Options) error
- func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, opts ...Options) error
- func Read(filenames ...string) (envMap map[string]string, err error)
- func Unmarshal(str string) (envMap map[string]string, err error)
- func Write(envMap map[string]string, filename string) error
- type Config
- type Env
- func (e *Env) Add(key, value string) (err error)
- func (e *Env) AddMany(keyValues ...string) (err error)
- func (e *Env) Find(key string) (value string, exists bool)
- func (e *Env) FindAll() (result map[string]string)
- func (e *Env) Get(key string) string
- func (e *Env) GetDefault(key, defaultValue string) string
- func (e *Env) GetInt(key string) int
- func (e *Env) Load(filenames ...string) (err error)
- func (e *Env) Parse(v interface{}, opts ...Options) (err error)
- func (e *Env) ParseConfig(v interface{}, opts ...Options) error
- func (e *Env) Read(filename string) (err error)
- func (e *Env) ReadDefault() (err error)
- func (e *Env) ReadFiles(filenames ...string) (err error)
- func (e *Env) Remove(key string) (err error)
- func (e *Env) RemoveMany(keys ...string) (err error)
- func (e *Env) Save(filename string) (err error)
- func (e *Env) Write(filename string) (err error)
- func (e *Env) WriteNew(filename string, envMap map[string]string) (err error)
- type OnSetFn
- type Options
- type ParserFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotAStructPtr is returned if you pass something that is not a pointer to a // Struct to Parse. ErrNotAStructPtr = errors.New("env: expected a pointer to a Struct") )
nolint: gochecknoglobals
Functions ¶
func Exec ¶ added in v0.1.1
Exec 加载环境变量文件列表,然后执行CMD命令 @param filenames 环境变量文件列表 @param cmd 命令 @param cmd cmdArgs 参数
func Marshal ¶ added in v0.1.1
Marshal outputs the given environment as a dotenv-formatted environment file. Each line is in the format: KEY="VALUE" where VALUE is backslash-escaped.
func Overload ¶ added in v0.1.1
Overload will read your env file(s) and load them into ENV for this process.
Call this function as close as possible to the start of your program (ideally in main)
If you call Overload without any args it will default to loading .env in the current path ¶
You can otherwise tell it which files to load (there can be more than one) like
godotenv.Overload("fileone", "filetwo")
It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefilly set all vars.
func ParseConfig ¶ added in v0.1.1
ParseConfig 从环境变量读取env配置写入到env标签对应的内容
func ParseWithFuncs ¶ added in v0.1.1
func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, opts ...Options) error
ParseWithFuncs is the same as `Parse` except it also allows the user to pass in custom parsers.
func Read ¶ added in v0.1.1
Read 读取所有文件中的环境变量 @param filenames 环境变量文件列表 @return envMap 环境变量字典 @return err 错误信息
Types ¶
type Config ¶
type Config struct { Debug bool `json:"debug" yaml:"debug" env:"debug"` ConfigFilePath string `json:"config_file_path" yaml:"config_file_path" env:"config_file_path"` }
func GetDefaultConfig ¶ added in v0.1.1
GetDefaultConfig 获取默认配置
func UpdateDefaultConfig ¶ added in v0.1.1
UpdateDefaultConfig 更新默认配置
type Env ¶
type Env struct { Config *Config // 核心配置 FileEnvMap map[string]map[string]string // 文件环境变量 SystemEnvMap map[string]string // 系统环境变量 AllEnvMap map[string]string // 所有环境变量 }
Env 操作环境变量的核心对象
func NewWithConfig ¶ added in v0.1.1
NewWithConfig 使用配置创建环境变量实例
func (*Env) GetDefault ¶
GetDefault 获取环境变量信息,找不到则使用默认值
func (*Env) Parse ¶ added in v0.1.1
Parse 解析带env标签的结构体,并将其值添加到环境变量中 @param v 结构体对象 @param opts 参数选项 @return opts 参数选项
func (*Env) ParseConfig ¶ added in v0.1.1
ParseConfig 从环境变量读取env配置写入到env标签对应的内容
func (*Env) ReadDefault ¶ added in v0.1.1
ReadDefault 读取默认的环境变量配置文件
func (*Env) RemoveMany ¶ added in v0.1.1
RemoveMany 同时移除多个环境变量
type Options ¶ added in v0.1.1
type Options struct { // Environment keys and values that will be accessible for the service. Environment map[string]string // TagName specifies another tagname to use rather than the default env. TagName string // RequiredIfNoDef automatically sets all env as required if they do not declare 'envDefault' RequiredIfNoDef bool // OnSet allows to run a function when a value is set OnSet OnSetFn // Prefix define a prefix for each key Prefix string // contains filtered or unexported fields }
Options for the parser.
type ParserFunc ¶ added in v0.1.1
ParserFunc defines the signature of a function that can be used within `CustomParsers`.