go env 源码

2022-07-15 浏览 (921)

golang env 代码

文件路径:/src/cmd/go/internal/base/env.go

/ Copyright 2017 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.

package base

import (
	"cmd/go/internal/cfg"
	"fmt"
	"os"
	"path/filepath"
	"runtime"
)

/ AppendPWD returns the result of appending PWD=dir to the environment base.
/
/ The resulting environment makes os.Getwd more efficient for a subprocess
/ running in dir, and also improves the accuracy of paths relative to dir
/ if one or more elements of dir is a symlink.
func AppendPWD(base []string, dir string) []string {
	/ POSIX requires PWD to be absolute.
	/ Internally we only use absolute paths, so dir should already be absolute.
	if !filepath.IsAbs(dir) {
		panic(fmt.Sprintf("AppendPWD with relative path %q", dir))
	}
	return append(base, "PWD="+dir)
}

/ AppendPATH returns the result of appending PATH=$GOROOT/bin:$PATH
/ (or the platform equivalent) to the environment base.
func AppendPATH(base []string) []string {
	if cfg.GOROOTbin == "" {
		return base
	}

	pathVar := "PATH"
	if runtime.GOOS == "plan9" {
		pathVar = "path"
	}

	path := os.Getenv(pathVar)
	if path == "" {
		return append(base, pathVar+"="+cfg.GOROOTbin)
	}
	return append(base, pathVar+"="+cfg.GOROOTbin+string(os.PathListSeparator)+path)
}

相关信息

go 源码目录

相关文章

go base 源码

go flag 源码

go goflags 源码

go path 源码

go signal 源码

go signal_notunix 源码

go signal_unix 源码

go tool 源码

^