Skip to main content
Version: 2.10.x(Latest)

Environment variable management component.

Usage:

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

API Documentation:

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

SetMap​

func SetMap(m map[string]string) error

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

genv.SetMap(g.MapStrStr{
"APPID": "order",
"THREAD": "16",
"ENDPOINTS": "127.0.0.1:6379",
})

GetWithCmd​

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​

func All() []string

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

Map​

func Map() map[string]string

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

Get​

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​

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​

func SetMap(m map[string]string) error

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

Contains​

func Contains(key string) bool

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

Remove​

func Remove(key ...string) error

This method can delete one or more environment variables.

Build​

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

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