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
// 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 {
}
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];
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;
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");
}
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 "."
}
}
-// 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 {
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 {
// 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 {
// $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:
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);
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);