1 Star 0 Fork 0

谢官峰/procs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
env.go 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
Eric Larson 提交于 2017-12-23 23:00 . Add comments on exported fields.
package procs
import (
"fmt"
"os"
"strings"
)
// ParseEnv takes an environment []string and converts it to a map[string]string.
func ParseEnv(environ []string) map[string]string {
env := make(map[string]string)
for _, e := range environ {
pair := strings.SplitN(e, "=", 2)
// There is a chance we can get an env with empty values
if len(pair) == 2 {
env[pair[0]] = pair[1]
}
}
return env
}
// Env takes a map[string]string and converts it to a []string that
// can be used with exec.Cmd. The useEnv boolean flag will include the
// current process environment, overlaying the provided env
// map[string]string.
func Env(env map[string]string, useEnv bool) []string {
envlist := []string{}
// update our env by loading our env and overriding any values in
// the provided env.
if useEnv {
environ := ParseEnv(os.Environ())
for k, v := range env {
environ[k] = v
}
env = environ
}
for key, val := range env {
if key == "" {
continue
}
envlist = append(envlist, fmt.Sprintf("%s=%s", key, val))
}
return envlist
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mars79668/procs.git
git@gitee.com:mars79668/procs.git
mars79668
procs
procs
v0.1.0

搜索帮助