github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/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 }