]> Cypherpunks repositories - gostls13.git/commitdiff
Getenv: almost no one wants the error, so make it return a string that may be empty.
authorRob Pike <r@golang.org>
Sat, 27 Jun 2009 03:28:41 +0000 (20:28 -0700)
committerRob Pike <r@golang.org>
Sat, 27 Jun 2009 03:28:41 +0000 (20:28 -0700)
Getenverror is the new name for the old routine that returns an error too.

R=rsc
DELTA=35  (7 added, 7 deleted, 21 changed)
OCL=30818
CL=30821

src/cmd/gobuild/gobuild.go
src/cmd/gobuild/util.go
src/cmd/godoc/godoc.go
src/pkg/exec/exec.go
src/pkg/os/env.go
src/pkg/os/getwd.go
src/pkg/time/zoneinfo.go
test/env.go

index a6497425cc2e3f5032b26f41ef03eeb6b7751e6e..6dce9d576f0bdc3fdd1354137b208328867fe745 100644 (file)
@@ -111,13 +111,13 @@ func (a FileArray) Swap(i, j int) {
 // If current directory is under $GOROOT/src/pkg, return the
 // path relative to there.  Otherwise return "".
 func PkgDir() string {
-       goroot, err := os.Getenv("GOROOT");
-       if err != nil || goroot == "" {
+       goroot := os.Getenv("GOROOT");
+       if goroot == "" {
                return ""
        }
        srcroot := path.Clean(goroot + "/src/pkg/");
-       pwd, err1 := os.Getenv("PWD");  // TODO(rsc): real pwd
-       if err1 != nil || pwd == "" {
+       pwd := os.Getenv("PWD");        // TODO(rsc): real pwd
+       if pwd == "" {
                return ""
        }
        if pwd == srcroot {
index 4bcf97a5a637bb6a066bd8506adf7be06b8221fb..99539f76dec74f1024d417c9bb5c4250209eb094 100644 (file)
@@ -45,9 +45,8 @@ func fatal(format string, args ...) {
 }
 
 func init() {
-       var err os.Error;
-       goarch, err = os.Getenv("GOARCH");
-       goos, err = os.Getenv("GOOS");
+       goarch = os.Getenv("GOARCH");
+       goos = os.Getenv("GOOS");
 
        var ok bool;
        theChar, ok = theChars[goarch];
@@ -64,7 +63,7 @@ func init() {
 
        for i, v := range binaries {
                var s string;
-               if s, err = exec.LookPath(v); err != nil {
+               if s, err := exec.LookPath(v); err != nil {
                        fatal("cannot find binary %s", v);
                }
                bin[v] = s;
index 12a21b6e20232b21fa4afb605cede8aef0ef2844..4e684aa6eeb201314d3980809a4365cf1d0468b2 100644 (file)
@@ -97,9 +97,8 @@ var (
 
 
 func init() {
-       var err os.Error;
-       goroot, err = os.Getenv("GOROOT");
-       if err != nil {
+       goroot = os.Getenv("GOROOT");
+       if goroot != "" {
                goroot = "/home/r/go-release/go";
        }
        flag.StringVar(&goroot, "goroot", goroot, "Go root directory");
index 7ddb98b50881f6338ef0a83d72d1519299d5c2db..a50f9dc13a16db7129032db3d39da2fdfbb710b2 100644 (file)
@@ -208,12 +208,7 @@ func LookPath(file string) (string, os.Error) {
                }
                return "", os.ENOENT;
        }
-       pathenv, err := os.Getenv("PATH");
-       if err != nil {
-               // Unix shell semantics: no $PATH means assume PATH=""
-               // (equivalent to PATH=".").
-               pathenv = "";
-       }
+       pathenv := os.Getenv("PATH");
        for i, dir := range strings.Split(pathenv, ":", 0) {
                if dir == "" {
                        // Unix shell semantics: path element "" means "."
index 4dbc2a4883349fdb823b4dbb099d95281f05bee6..3bd0fa9fea30d23c504c8b7f0dad40be528b4482 100644 (file)
@@ -29,9 +29,9 @@ func copyenv() {
        }
 }
 
-// Getenv retrieves the value of the environment variable named by the key.
+// Getenverror retrieves the value of the environment variable named by the key.
 // It returns the value and an error, if any.
-func Getenv(key string) (value string, err Error) {
+func Getenverror(key string) (value string, err Error) {
        once.Do(copyenv);
 
        if len(key) == 0 {
@@ -44,6 +44,13 @@ func Getenv(key string) (value string, err Error) {
        return v, 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);
+       return v;
+}
+
 // Setenv sets the value of the environment variable named by the key.
 // It returns an Error, if any.
 func Setenv(key, value string) Error {
index cbc6134a7d01f5dadb95546617ca8f34f0fb7256..5b1b4e2e285c742091bfe17909603d8303e48172 100644 (file)
@@ -28,7 +28,7 @@ func Getwd() (string, Error) {
 
        // Clumsy but widespread kludge:
        // if $PWD is set and matches ".", use it.
-       pwd, _ := Getenv("PWD");
+       pwd:= Getenv("PWD");
        if len(pwd) > 0 && pwd[0] == '/' {
                d, err := Stat(pwd);
                if err == nil && d.Dev == dot.Dev && d.Ino == dot.Ino {
index e2102f1dedfdd6ae48850793f3c308a4f029db3c..a4717c445c8de35d3ac9ed7216d41e4f70247470 100644 (file)
@@ -212,7 +212,7 @@ func setupZone() {
        // $TZ="" means use UTC.
        // $TZ="foo" means use /usr/share/zoneinfo/foo.
 
-       tz, err := os.Getenv("TZ");
+       tz, err := os.Getenverror("TZ");
        var ok bool;
        switch {
        case err == os.ENOENV:
index db76ee49f31d903aac93ad21f8dd331a3fa11e80..2cf9ddf381373f74ce8f3542a23b7665d80e8583 100644 (file)
@@ -9,7 +9,7 @@ package main
 import os "os"
 
 func main() {
-       ga, e0 := os.Getenv("GOARCH");
+       ga, e0 := os.Getenverror("GOARCH");
        if e0 != nil {
                print("$GOARCH: ", e0.String(), "\n");
                os.Exit(1);
@@ -18,7 +18,7 @@ func main() {
                print("$GOARCH=", ga, "\n");
                os.Exit(1);
        }
-       xxx, e1 := os.Getenv("DOES_NOT_EXIST");
+       xxx, e1 := os.Getenverror("DOES_NOT_EXIST");
        if e1 != os.ENOENV {
                print("$DOES_NOT_EXIST=", xxx, "; err = ", e1.String(), "\n");
                os.Exit(1);