2020-07-26 17:09:05 +08:00
|
|
|
package proc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
envs = make(map[string]string)
|
|
|
|
envLock sync.RWMutex
|
|
|
|
)
|
|
|
|
|
2021-02-22 10:07:39 +08:00
|
|
|
// Env returns the value of the given environment variable.
|
2020-07-26 17:09:05 +08:00
|
|
|
func Env(name string) string {
|
|
|
|
envLock.RLock()
|
|
|
|
val, ok := envs[name]
|
|
|
|
envLock.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
val = os.Getenv(name)
|
|
|
|
envLock.Lock()
|
|
|
|
envs[name] = val
|
|
|
|
envLock.Unlock()
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:07:39 +08:00
|
|
|
// EnvInt returns an int value of the given environment variable.
|
2020-07-26 17:09:05 +08:00
|
|
|
func EnvInt(name string) (int, bool) {
|
|
|
|
val := Env(name)
|
|
|
|
if len(val) == 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := strconv.Atoi(val)
|
|
|
|
if err != nil {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, true
|
|
|
|
}
|