golang中的6大类环境变量使用说明和总结

        在go语言中,环境变量可是很重要的,go命令及其调用的工具会参考环境变量进行配置。如果环境变量未设置或为空,go命令将使用合理的默认设置。在go语言中一共有6大类的环境变量,他们分别是通用环境变量, cgo专用的环境变量, 特定的系统架构中的环境变量,用于代码覆盖率的环境变量, 特殊用途环境变量,其他可从“go env”获得,但不能从环境中读取的变量。

        其中比较重要,而且我们经常用到的环境变量有:

  •  GOPATH 这个是go的运行路径, 非常重要,一般设置为go项目的根路径,这个下面会自动生成 bin ,pkg 和 src 文件夹, 他们都是你的go项目运行中比较重要的文件夹。 bin文件夹下保存了所有通过go install安装的工具的可执行命令,可通过GOBIN修改, pkg里面是一些依赖的库文件,如xx.a文件, src 是你下载的依赖的源码文件 
  • GOBIN  go的可执行路径,默认位于GOPATH/bin 目录,这个路径决定了你执行go install后的编译文件的安装路径,需要将他加入到你的系统PATH环境变量中;
  •  GO111MODULE 这个是用来控制go的运行模式是模块方式还是GOPATH模式;
  •  GOARCH 系统架构,如 amd64, 386, arm, ppc64 , 这个配置不合适代码直接无法运行;
  •  GOOS 这个也是一个很常用的变量,他返回当前操作系统的名称, 如 linux, darwin, windows, netbsd;
  • 还有一个在打包编译的时候经常用到 CGO_ENABLED 是否启用cgo, 一般都是0 禁用。

环境变量查看方法

查看当前系统中所有的go相关环境变量: go env

查看某个环境变量 <NAME>的有效设置,请运行 go env <NAME>

环境变量更改方法

要更改默认设置,请运行 go env -w <NAME>=<VALUE>    根据操作系统的提示,使用'go env -w '更改的默认值将记录在每个用户配置目录中存储的go环境配置文件中, 配置文件路径可使用 os.UserConfigDir 查看。配置文件的位置可以通过设置环境变量GOENV来更改,通过 go env GOENV 查看环境变量的位置,但 go env -w  无法更改默认位置。有关详细信息,请参阅“go help env”。

6大类环境变量详情

这些个环境变量的英文很简单,为了保持“原汁原味”就不翻译了,直接用了官方的英文描述。

通用环境变量:

General-purpose environment variables:
GO111MODULE
    Controls whether the go command runs in module-aware mode or GOPATH mode.
    May be "off", "on", or "auto".
    See https://golang.org/ref/mod#mod-commands.
GCCGO
    The gccgo command to run for 'go build -compiler=gccgo'.
GOARCH
    The architecture, or processor, for which to compile code.
    Examples are amd64, 386, arm, ppc64.
GOBIN
    The directory where 'go install' will install a command.
GOCACHE
    The directory where the go command will store cached
    information for reuse in future builds.
GOMODCACHE
    The directory where the go command will store downloaded modules.
GODEBUG
    Enable various debugging facilities. See https://go.dev/doc/godebug
    for details.
GOENV
    The location of the Go environment configuration file.
    Cannot be set using 'go env -w'.
    Setting GOENV=off in the environment disables the use of the
    default configuration file.
GOFLAGS
    A space-separated list of -flag=value settings to apply
    to go commands by default, when the given flag is known by
    the current command. Each entry must be a standalone flag.
    Because the entries are space-separated, flag values must
    not contain spaces. Flags listed on the command line
    are applied after this list and therefore override it.
GOINSECURE
    Comma-separated list of glob patterns (in the syntax of Go's path.Match)
    of module path prefixes that should always be fetched in an insecure
    manner. Only applies to dependencies that are being fetched directly.
    GOINSECURE does not disable checksum database validation. GOPRIVATE or
    GONOSUMDB may be used to achieve that.
GOOS
    The operating system for which to compile code.
    Examples are linux, darwin, windows, netbsd.
GOPATH
    Controls where various files are stored. See: 'go help gopath'.
GOPROXY
    URL of Go module proxy. See https://golang.org/ref/mod#environment-variables
    and https://golang.org/ref/mod#module-proxy for details.
GOPRIVATE, GONOPROXY, GONOSUMDB
    Comma-separated list of glob patterns (in the syntax of Go's path.Match)
    of module path prefixes that should always be fetched directly
    or that should not be compared against the checksum database.
    See https://golang.org/ref/mod#private-modules.
GOROOT
    The root of the go tree.
GOSUMDB
    The name of checksum database to use and optionally its public key and
    URL. See https://golang.org/ref/mod#authenticating.
GOTOOLCHAIN
    Controls which Go toolchain is used. See https://go.dev/doc/toolchain.
GOTMPDIR
    The directory where the go command will write
    temporary source files, packages, and binaries.
GOVCS
    Lists version control commands that may be used with matching servers.
    See 'go help vcs'.
GOWORK
    In module aware mode, use the given go.work file as a workspace file.
    By default or when GOWORK is "auto", the go command searches for a
    file named go.work in the current directory and then containing directories
    until one is found. If a valid go.work file is found, the modules
    specified will collectively be used as the main modules. If GOWORK
    is "off", or a go.work file is not found in "auto" mode, workspace
    mode is disabled.


cgo使用的环境变量:

Environment variables for use with cgo:


AR
    The command to use to manipulate library archives when
    building with the gccgo compiler.
    The default is 'ar'.
CC
    The command to use to compile C code.
CGO_ENABLED
    Whether the cgo command is supported. Either 0 or 1.
CGO_CFLAGS
    Flags that cgo will pass to the compiler when compiling
    C code.
CGO_CFLAGS_ALLOW
    A regular expression specifying additional flags to allow
    to appear in #cgo CFLAGS source code directives.
    Does not apply to the CGO_CFLAGS environment variable.
CGO_CFLAGS_DISALLOW
    A regular expression specifying flags that must be disallowed
    from appearing in #cgo CFLAGS source code directives.
    Does not apply to the CGO_CFLAGS environment variable.
CGO_CPPFLAGS, CGO_CPPFLAGS_ALLOW, CGO_CPPFLAGS_DISALLOW
    Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
    but for the C preprocessor.
CGO_CXXFLAGS, CGO_CXXFLAGS_ALLOW, CGO_CXXFLAGS_DISALLOW
    Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
    but for the C++ compiler.
CGO_FFLAGS, CGO_FFLAGS_ALLOW, CGO_FFLAGS_DISALLOW
    Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
    but for the Fortran compiler.
CGO_LDFLAGS, CGO_LDFLAGS_ALLOW, CGO_LDFLAGS_DISALLOW
    Like CGO_CFLAGS, CGO_CFLAGS_ALLOW, and CGO_CFLAGS_DISALLOW,
    but for the linker.
CXX
    The command to use to compile C++ code.
FC
    The command to use to compile Fortran code.
PKG_CONFIG
    Path to pkg-config tool.


特定的系统架构中的环境变量

Architecture-specific environment variables:

GOARM
    For GOARCH=arm, the ARM architecture for which to compile.
    Valid values are 5, 6, 7.
    The value can be followed by an option specifying how to implement floating point instructions.
    Valid options are ,softfloat (default for 5) and ,hardfloat (default for 6 and 7).
GO386
    For GOARCH=386, how to implement floating point instructions.
    Valid values are sse2 (default), softfloat.
GOAMD64
    For GOARCH=amd64, the microarchitecture level for which to compile.
    Valid values are v1 (default), v2, v3, v4.
    See https://golang.org/wiki/MinimumRequirements#amd64
GOMIPS
    For GOARCH=mips{,le}, whether to use floating point instructions.
    Valid values are hardfloat (default), softfloat.
GOMIPS64
    For GOARCH=mips64{,le}, whether to use floating point instructions.
    Valid values are hardfloat (default), softfloat.
GOPPC64
    For GOARCH=ppc64{,le}, the target ISA (Instruction Set Architecture).
    Valid values are power8 (default), power9, power10.
GOWASM
    For GOARCH=wasm, comma-separated list of experimental WebAssembly features to use.
    Valid values are satconv, signext.

用于代码覆盖率的环境变量:

Environment variables for use with code coverage:

GOCOVERDIR
    Directory into which to write code coverage data files
    generated by running a "go build -cover" binary.
    Requires that GOEXPERIMENT=coverageredesign is enabled.


特殊用途环境变量:

Special-purpose environment variables:

GCCGOTOOLDIR
    If set, where to find gccgo tools, such as cgo.
    The default is based on how gccgo was configured.
GOEXPERIMENT
    Comma-separated list of toolchain experiments to enable or disable.
    The list of available experiments may change arbitrarily over time.
    See src/internal/goexperiment/flags.go for currently valid values.
    Warning: This variable is provided for the development and testing
    of the Go toolchain itself. Use beyond that purpose is unsupported.
GOROOT_FINAL
    The root of the installed Go tree, when it is
    installed in a location other than where it is built.
    File names in stack traces are rewritten from GOROOT to
    GOROOT_FINAL.
GO_EXTLINK_ENABLED
    Whether the linker should use external linking mode
    when using -linkmode=auto with code that uses cgo.
    Set to 0 to disable external linking mode, 1 to enable it.
GIT_ALLOW_PROTOCOL
    Defined by Git. A colon-separated list of schemes that are allowed
    to be used with git fetch/clone. If set, any scheme not explicitly
    mentioned will be considered insecure by 'go get'.
    Because the variable is defined by Git, the default value cannot
    be set using 'go env -w'.


其他可从“go env”获得,但不能从环境中读取的信息:

Additional information available from 'go env' but not read from the environment:

GOEXE
    The executable file name suffix (".exe" on Windows, "" on other systems).
GOGCCFLAGS
    A space-separated list of arguments supplied to the CC command.
GOHOSTARCH
    The architecture (GOARCH) of the Go toolchain binaries.
GOHOSTOS
    The operating system (GOOS) of the Go toolchain binaries.
GOMOD
    The absolute path to the go.mod of the main module.
    If module-aware mode is enabled, but there is no go.mod, GOMOD will be
    os.DevNull ("/dev/null" on Unix-like systems, "NUL" on Windows).
    If module-aware mode is disabled, GOMOD will be the empty string.
GOTOOLDIR
    The directory where the go tools (compile, cover, doc, etc...) are installed.
GOVERSION
    The version of the installed Go tree, as reported by runtime.Version.

参考:go command - cmd/go - Go Packages

  • 34
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值