github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/vars/env.go (about) 1 package vars 2 3 import ( 4 "errors" 5 "os" 6 ) 7 8 var errEnvMustBeString = errors.New("environment variable can only be set string values") 9 10 // envVariable represents an environment variable. 11 type envVariable struct { 12 name string 13 } 14 15 func (ev envVariable) Set(val interface{}) error { 16 if s, ok := val.(string); ok { 17 os.Setenv(ev.name, string(s)) 18 return nil 19 } 20 return errEnvMustBeString 21 } 22 23 func (ev envVariable) Get() interface{} { 24 return string(os.Getenv(ev.name)) 25 } 26 27 // NewEnv returns an environment variable. 28 func NewEnv(name string) Var { 29 return envVariable{name} 30 }