env.go 510 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package proc
  2. import (
  3. "os"
  4. "strconv"
  5. "sync"
  6. )
  7. var (
  8. envs = make(map[string]string)
  9. envLock sync.RWMutex
  10. )
  11. func Env(name string) string {
  12. envLock.RLock()
  13. val, ok := envs[name]
  14. envLock.RUnlock()
  15. if ok {
  16. return val
  17. }
  18. val = os.Getenv(name)
  19. envLock.Lock()
  20. envs[name] = val
  21. envLock.Unlock()
  22. return val
  23. }
  24. func EnvInt(name string) (int, bool) {
  25. val := Env(name)
  26. if len(val) == 0 {
  27. return 0, false
  28. }
  29. n, err := strconv.Atoi(val)
  30. if err != nil {
  31. return 0, false
  32. }
  33. return n, true
  34. }