Source File
env.go
Belonging Package
os
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// General environment variables.
package os
import (
)
// Expand replaces ${var} or $var in the string based on the mapping function.
// For example, os.ExpandEnv(s) is equivalent to os.Expand(s, os.Getenv).
func ( string, func(string) string) string {
var []byte
// ${} is all ASCII, so bytes are fine for this operation.
:= 0
for := 0; < len(); ++ {
if [] == '$' && +1 < len() {
if == nil {
= make([]byte, 0, 2*len())
}
= append(, [:]...)
, := getShellName([+1:])
if == "" && > 0 {
// Encountered invalid syntax; eat the
// characters.
} else if == "" {
// Valid syntax, but $ was not followed by a
// name. Leave the dollar character untouched.
= append(, [])
} else {
= append(, ()...)
}
+=
= + 1
}
}
if == nil {
return
}
return string() + [:]
}
// ExpandEnv replaces ${var} or $var in the string according to the values
// of the current environment variables. References to undefined
// variables are replaced by the empty string.
func ( string) string {
return Expand(, Getenv)
}
// isShellSpecialVar reports whether the character identifies a special
// shell variable such as $*.
func ( uint8) bool {
switch {
case '*', '#', '$', '@', '!', '?', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return true
}
return false
}
// isAlphaNum reports whether the byte is an ASCII letter, number, or underscore
func ( uint8) bool {
return == '_' || '0' <= && <= '9' || 'a' <= && <= 'z' || 'A' <= && <= 'Z'
}
// getShellName returns the name that begins the string and the number of bytes
// consumed to extract it. If the name is enclosed in {}, it's part of a ${}
// expansion and two more bytes are needed than the length of the name.
func ( string) (string, int) {
switch {
case [0] == '{':
if len() > 2 && isShellSpecialVar([1]) && [2] == '}' {
return [1:2], 3
}
// Scan to closing brace
for := 1; < len(); ++ {
if [] == '}' {
if == 1 {
return "", 2 // Bad syntax; eat "${}"
}
return [1:], + 1
}
}
return "", 1 // Bad syntax; eat "${"
case isShellSpecialVar([0]):
return [0:1], 1
}
// Scan alphanumerics.
var int
for = 0; < len() && isAlphaNum([]); ++ {
}
return [:],
}
// 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.
// To distinguish between an empty value and an unset value, use LookupEnv.
func ( string) string {
testlog.Getenv()
, := syscall.Getenv()
return
}
// LookupEnv retrieves the value of the environment variable named
// by the key. If the variable is present in the environment the
// value (which may be empty) is returned and the boolean is true.
// Otherwise the returned value will be empty and the boolean will
// be false.
func ( string) (string, bool) {
testlog.Getenv()
return syscall.Getenv()
}
// Setenv sets the value of the environment variable named by the key.
// It returns an error, if any.
func (, string) error {
:= syscall.Setenv(, )
if != nil {
return NewSyscallError("setenv", )
}
return nil
}
// Unsetenv unsets a single environment variable.
func ( string) error {
return syscall.Unsetenv()
}
// Clearenv deletes all environment variables.
func () {
syscall.Clearenv()
}
// Environ returns a copy of strings representing the environment,
// in the form "key=value".
func () []string {
return syscall.Environ()
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)