Environment variable management component.

Usage:

  1. import "github.com/gogf/gf/v2/os/genv"

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/os/genv

SetMap

  1. func SetMap(m map[string]string) error

This method is used for batch setting of environment variables. Usage example:

  1. genv.SetMap(g.MapStrStr{
  2. "APPID": "order",
  3. "THREAD": "16",
  4. "ENDPOINTS": "127.0.0.1:6379",
  5. })

GetWithCmd

  1. func GetWithCmd(key string, def ...interface{}) *gvar.Var

This method is used to get the specified option value in the environment variable. If the environment variable does not exist, it will read from the command-line option. However, the naming rules for both will be different. For example: genv.GetWithCmd("gf.debug") will first try to read the value of the GF_DEBUG environment variable, and if it does not exist, it will go to the gf.debug option in the command line.

Note the naming conversion rules:

  • Environment variables convert names to uppercase and . characters to _ characters.
  • In the command line, names are converted to lowercase and _ characters to . characters.

All

  1. func All() []string

This method returns the string in the environment variable and returns it in the form of key=value.

Map

  1. func Map() map[string]string

This method returns the string in the environment variable and returns it in the form of map.

Get

  1. func Get(key string, def ...interface{}) *gvar.Var

This method is used to create a generic type environment variable. If the given key does not exist, it returns a default generic type environment variable.

Set

  1. func Set(key, value string) error

This method sets environment variables with the given key and value, and returns an Error type if there is an error.

SetMap

  1. func SetMap(m map[string]string) error

This method stores environment variables through a parameter of type map.

Contains

  1. func Contains(key string) bool

This method checks whether the key exists in the environment variable.

Remove

  1. func Remove(key ...string) error

This method can delete one or more environment variables.

Build

  1. func Build(m map[string]string) []string

This method constructs and returns the map parameter in the form of an array.