1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Unix environment variables.
13 // ENOENV is the Error indicating that an environment variable does not exist.
14 var ENOENV = NewError("no such environment variable")
16 var env map[string]string
20 env = make(map[string]string)
21 for _, s := range Envs {
22 for j := 0; j < len(s); j++ {
31 var envLock sync.RWMutex
33 // Getenverror retrieves the value of the environment variable named by the key.
34 // It returns the value and an error, if any.
35 func Getenverror(key string) (value string, err Error) {
43 defer envLock.RUnlock()
52 // Getenv retrieves the value of the environment variable named by the key.
53 // It returns the value, which will be empty if the variable is not present.
54 func Getenv(key string) string {
55 v, _ := Getenverror(key)
59 // Setenv sets the value of the environment variable named by the key.
60 // It returns an Error, if any.
61 func Setenv(key, value string) Error {
68 defer envLock.Unlock()
71 setenv_c(key, value) // is a no-op if cgo isn't loaded
75 // Clearenv deletes all environment variables.
77 once.Do(copyenv) // prevent copyenv in Getenv/Setenv
80 defer envLock.Unlock()
82 env = make(map[string]string)
84 // TODO(bradfitz): pass through to C
87 // Environ returns an array of strings representing the environment,
88 // in the form "key=value".
89 func Environ() []string {
92 defer envLock.RUnlock()
93 a := make([]string, len(env))
95 for k, v := range env {
102 // TempDir returns the default directory to use for temporary files.
103 func TempDir() string {
104 dir := Getenv("TMPDIR")