environ.go

 1package uv
 2
 3import (
 4	"strings"
 5)
 6
 7// Environ is a slice of strings that represents the environment variables of
 8// the program.
 9type Environ []string
10
11// Getenv returns the value of the environment variable named by the key. If
12// the variable is not present in the environment, the value returned will be
13// the empty string.
14func (p Environ) Getenv(key string) (v string) {
15	v, _ = p.LookupEnv(key)
16	return
17}
18
19// LookupEnv retrieves the value of the environment variable named by the key.
20// If the variable is present in the environment the value (which may be empty)
21// is returned and the boolean is true. Otherwise the returned value will be
22// empty and the boolean will be false.
23func (p Environ) LookupEnv(key string) (s string, v bool) {
24	for i := len(p) - 1; i >= 0; i-- {
25		if strings.HasPrefix(p[i], key+"=") {
26			s = strings.TrimPrefix(p[i], key+"=")
27			v = true
28			break
29		}
30	}
31	return
32}