<a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a>
and
<a href="/pkg/os/#ErrNoEnv"><code>ErrNoEnv</code></a>.
+</p>
+<p>
+The <code>Getenverror</code> function has been removed. To distinguish
+between a non-existent environment variable and an empty string,
+use <a href="/pkg/os/#Environ"><code>os.Environ</code></a> or
+<a href="/pkg/syscall/#Getenv"><code>syscall.Getenv</code></a>.
+</p>
<p>
<em>Updating</em>:
<a href="/pkg/os/#ErrPermission"><code>ErrPermission</code></a>
and
<a href="/pkg/os/#ErrNoEnv"><code>ErrNoEnv</code></a>.
+</p>
+<p>
+The <code>Getenverror</code> function has been removed. To distinguish
+between a non-existent environment variable and an empty string,
+use <a href="/pkg/os/#Environ"><code>os.Environ</code></a> or
+<a href="/pkg/syscall/#Getenv"><code>syscall.Getenv</code></a>.
+</p>
<p>
<em>Updating</em>:
"GOROOT_FINAL=/usr/local/go",
}
for _, k := range extraEnv {
- s, err := os.Getenverror(k)
- if err == nil {
+ if s, ok := getenvOk(k); ok {
e = append(e, k+"="+s)
}
}
"GOBUILDEXIT": "1", // exit all.bat with completion status.
}
for _, name := range extraEnv {
- s, err := os.Getenverror(name)
- if err == nil {
+ if s, ok := getenvOk(name); ok {
start[name] = s
}
}
}
return ".bash"
}
+
+func getenvOk(k string) (v string, ok bool) {
+ v = os.Getenv(k)
+ if v != "" {
+ return v, true
+ }
+ keq := k + "="
+ for _, kv := range os.Environ() {
+ if kv == keq {
+ return "", true
+ }
+ }
+ return "", false
+}
package os
-import (
- "errors"
- "syscall"
-)
+import "syscall"
// Expand replaces ${var} or $var in the string based on the mapping function.
// Invocations of undefined variables are replaced with the empty string.
return s[:i], i
}
-// ENOENV is the error indicating that an environment variable does not exist.
-var ENOENV = errors.New("no such environment variable")
-
-// Getenverror retrieves the value of the environment variable named by the key.
-// It returns the value and an error, if any.
-func Getenverror(key string) (value string, err error) {
- if len(key) == 0 {
- return "", ErrInvalid
- }
- val, found := syscall.Getenv(key)
- if !found {
- return "", ENOENV
- }
- return val, nil
-}
-
// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present.
func Getenv(key string) string {
- v, _ := Getenverror(key)
+ v, _ := syscall.Getenv(key)
return v
}
)
func main() {
- ga, e0 := os.Getenverror("GOARCH")
- if e0 != nil {
- print("$GOARCH: ", e0.Error(), "\n")
- os.Exit(1)
- }
+ ga := os.Getenv("GOARCH")
if ga != runtime.GOARCH {
print("$GOARCH=", ga, "!= runtime.GOARCH=", runtime.GOARCH, "\n")
os.Exit(1)
}
- xxx, e1 := os.Getenverror("DOES_NOT_EXIST")
- if e1 != os.ENOENV {
- print("$DOES_NOT_EXIST=", xxx, "; err = ", e1.Error(), "\n")
+ xxx := os.Getenv("DOES_NOT_EXIST")
+ if xxx != "" {
+ print("$DOES_NOT_EXIST=", xxx, "\n")
os.Exit(1)
}
}