// 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 osimport ()// 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. := 0for := 0; < len(); ++ {if [] == '$' && +1 < len() {if == nil { = make([]byte, 0, 2*len()) } = append(, [:]...) , := getShellName([+1:])if == "" && > 0 {// Encountered invalid syntax; eat the // characters. } elseif == "" {// Valid syntax, but $ was not followed by a // name. Leave the dollar character untouched. = append(, []) } else { = append(, ()...) } += = + 1 } }if == nil {return }returnstring() + [:]}// 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 {returnExpand(, 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':returntrue }returnfalse}// 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] == '{':iflen() > 2 && isShellSpecialVar([1]) && [2] == '}' {return [1:2], 3 }// Scan to closing bracefor := 1; < len(); ++ {if [] == '}' {if == 1 {return"", 2// Bad syntax; eat "${}" }return [1:], + 1 } }return"", 1// Bad syntax; eat "${"caseisShellSpecialVar([0]):return [0:1], 1 }// Scan alphanumerics.varintfor = 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()returnsyscall.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 {returnNewSyscallError("setenv", ) }returnnil}// Unsetenv unsets a single environment variable.func ( string) error {returnsyscall.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 {returnsyscall.Environ()}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.