github.com/zhongdalu/gf@v1.0.0/g/os/genv/genv.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  // Package genv provides operations for environment variables of system.
     8  package genv
     9  
    10  import "os"
    11  
    12  // All returns a copy of strings representing the environment,
    13  // in the form "key=value".
    14  func All() []string {
    15  	return os.Environ()
    16  }
    17  
    18  // Get returns the value of the environment variable named by the <key>.
    19  // It returns given <def> if the variable does not exist in the environment.
    20  func Get(key string, def ...string) string {
    21  	v, ok := os.LookupEnv(key)
    22  	if !ok && len(def) > 0 {
    23  		return def[0]
    24  	}
    25  	return v
    26  }
    27  
    28  // Set sets the value of the environment variable named by the <key>.
    29  // It returns an error, if any.
    30  func Set(key, value string) error {
    31  	return os.Setenv(key, value)
    32  }
    33  
    34  // Remove deletes a single environment variable.
    35  func Remove(key string) error {
    36  	return os.Unsetenv(key)
    37  }