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 // +build darwin freebsd linux openbsd
7 // Unix environment variables.
16 // ENOENV is the error indicating that an environment variable does not exist.
17 var ENOENV = errors.New("no such environment variable")
19 var env map[string]string
23 env = make(map[string]string)
24 for _, s := range Envs {
25 for j := 0; j < len(s); j++ {
34 var envLock sync.RWMutex
36 // Getenverror retrieves the value of the environment variable named by the key.
37 // It returns the value and an error, if any.
38 func Getenverror(key string) (value string, err error) {
46 defer envLock.RUnlock()
55 // Getenv retrieves the value of the environment variable named by the key.
56 // It returns the value, which will be empty if the variable is not present.
57 func Getenv(key string) string {
58 v, _ := Getenverror(key)
62 // Setenv sets the value of the environment variable named by the key.
63 // It returns an error, if any.
64 func Setenv(key, value string) error {
71 defer envLock.Unlock()
74 setenv_c(key, value) // is a no-op if cgo isn't loaded
78 // Clearenv deletes all environment variables.
80 once.Do(copyenv) // prevent copyenv in Getenv/Setenv
83 defer envLock.Unlock()
85 env = make(map[string]string)
87 // TODO(bradfitz): pass through to C
90 // Environ returns an array of strings representing the environment,
91 // in the form "key=value".
92 func Environ() []string {
95 defer envLock.RUnlock()
96 a := make([]string, len(env))
98 for k, v := range env {
105 // TempDir returns the default directory to use for temporary files.
106 func TempDir() string {
107 dir := Getenv("TMPDIR")