michaelwilli

Go Config

Nov 23rd, 2020
1,301
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package config
  2.  
  3. import (
  4.     "os"
  5.     "sync"
  6. )
  7.  
  8. type Config struct {
  9.     Name string
  10. }
  11.  
  12. func (c *Config) Refresh() {
  13.     c.Name = os.Getenv("NAME")
  14.     println("Refreshing")
  15. }
  16.  
  17. var instance *Config
  18. var once sync.Once
  19.  
  20. func GetConfig() *Config {
  21.     once.Do(func() {
  22.         println("getting config")
  23.         instance = &Config{}
  24.         instance.Refresh()
  25.     })
  26.     return instance
  27. }
RAW Paste Data