Fixes #3065

R=golang-dev, dsymonds, rsc
CC=golang-dev
https://golang.org/cl/5675094
This commit is contained in:
Brad Fitzpatrick 2012-02-18 21:18:13 -08:00
Родитель 83feedf7bf
Коммит efacb2a1b4
5 изменённых файлов: 36 добавлений и 33 удалений

Просмотреть файл

@ -1451,7 +1451,14 @@ with more Go-like names, such as
<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>:

Просмотреть файл

@ -1354,7 +1354,14 @@ with more Go-like names, such as
<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>:

Просмотреть файл

@ -480,8 +480,7 @@ func (b *Builder) envv() []string {
"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)
}
}
@ -497,8 +496,7 @@ func (b *Builder) envvWindows() []string {
"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
}
}
@ -782,3 +780,17 @@ func defaultSuffix() string {
}
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
}

Просмотреть файл

@ -6,10 +6,7 @@
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.
@ -77,26 +74,10 @@ func getShellName(s string) (string, int) {
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
}

Просмотреть файл

@ -15,18 +15,14 @@ import (
)
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)
}
}