go语言有哪些环境变量
admin
2023-02-16 04:20:04
0

go语言有哪些环境变量?针对这个问题,今天小编总结这篇有关golang环境变量的文章,可供感兴趣的小伙伴们参考借鉴,希望对大家有所帮助。

环境变量代表的含义:

$ go help environment
The go command and the tools it invokes consult environment variables
for configuration. If an environment variable is unset, the go command
uses a sensible default setting. To see the effective setting of the
variable , run 'go env '. To change the default setting,
run 'go env -w ='. Defaults changed using 'go env -w'
are recorded in a Go environment configuration file stored in the
per-user configuration directory, as reported by os.UserConfigDir.
The location of the configuration file can be changed by setting
the environment variable GOENV, and 'go env GOENV' prints the
effective location, but 'go env -w' cannot change the default location.
See 'go help env' for details.

General-purpose environment variables:

        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.
        GODEBUG
                Enable various debugging facilities. See 'go doc runtime'
                for details.
        GOENV
                The location of the Go environment configuration file.
                Cannot be set using 'go env -w'.
        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.
        GOOS
                The operating system for which to compile code.
                Examples are linux, darwin, windows, netbsd.
        GOPATH
                For more details see: 'go help gopath'.
        GOPROXY
                URL of Go module proxy. See 'go help modules'.
        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 'go help module-private'.
        GOROOT
                The root of the go tree.
        GOSUMDB
                The name of checksum database to use and optionally its public key and
                URL. See 'go help module-auth'.
        GOTMPDIR
                The directory where the go command will write
                temporary source files, packages, and binaries.

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.
        GO386
                For GOARCH=386, the floating point instruction set.
                Valid values are 387, sse2.
        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.
        GOWASM
                For GOARCH=wasm, comma-separated list of experimental WebAssembly features to use.
                Valid values are satconv, signext.

Special-purpose environment variables:

        GCCGOTOOLDIR
                If set, where to find gccgo tools, such as cgo.
                The default is based on how gccgo was configured.
        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'.

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,
                or the empty string if not using modules.
        GOTOOLDIR
                The directory where the go tools (compile, cover, doc, etc...) are installed.

你可以使用以下命令查看你的go环境变量:

$ go env

接下来就来了解一下其他的环境变量。

GOARCH 和 GOOS

GOARCH :cpu架构,386、amd64、arm等,在交叉编译的时候设置,可以编译不同平台的包。
GOOS:平台,linux、windows等,通常和GOARCH搭配使用。
这里贴一下在不同平台下交叉编译的命令:

Mac下编译Linux, Windows平台的64位可执行程序:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build test.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go

Linux下编译Mac, Windows平台的64位可执行程序:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build test.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go

Windows下编译Mac, Linux平台的64位可执行程序:
SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build test.go
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build test.go

GOCACHE

这是go build产生的缓存,这可以加快编译速度,我在GOCACHE打印出来的路径下看到了一个README文件,它解释了GOCACHE。

This directory holds cached build artifacts from the Go build system.
Run "go clean -cache" if the directory is getting too large.
See golang.org to learn more about Go.

可以使用"go clean -cache"命令清除编译缓存。

GOENV

本地的go环境文件存储位置,不可以用"go env -w"设置它。

以上就是golang环境变量的知识汇总,内容较为全面,小编相信有部分知识点可能是我们日常工作可能会见到或用到的。希望你能通过这篇文章学到更多知识。

相关内容

热门资讯

馆校合作丨南充科技馆走进仪陇县... 馆校合作 南充科技馆走进 NCSTM 仪陇县实验学校 天府科普研学游 4月29日上午,南充科技馆科普...
我国本土发现的首块月球陨石有重... 我国本土发现的首块月球陨石揭示了月球两次关键地质事件,并发现一种月球新矿物。 2026年世界地球日,...
马斯克的GPU也在摸鱼?狂囤几... 新智元报道 编辑:元宇 【新智元导读】马斯克囤了几十万张卡,结果只跑了11%?据媒体报道,xAI的...
原创 特... 4月24日,白宫以总统人事办公室的名义,向美国国家科学委员会的22名在任委员群发了一封冷冰冰的电子邮...
以军总参谋长:若伊朗袭击以色列... △以色列国防军总参谋长扎米尔(资料图)当地时间5月5日,以色列国防军总参谋长扎米尔表示,如果伊朗在海...
神龙拜耳光伏有限公司生产支架 光伏支架,到底“支”起了什么? 提到光伏电站,很多人第一反应是那一块块深蓝色的电池板。但真正让这些电...
食品级制冰袋厂家选型指南:上海... 导语:在餐饮、物流、医药等行业中,制冰袋作为低温保鲜的核心耗材,其性能稳定性直接影响产品品质与运营效...
南通崇川发放首笔“算力券”补贴 购买、领券、付款……近日,随着在南通市公共算力服务平台算力超市完成中国移动算力服务的租用下单操作,江...
百万上下文之后,拼什么? 文 | 科技不许冷 过去几天,科技圈的视线全被DeepSeek吸走了。 满屏都是传闻中的估值溢价,...
成语之都 好玩邯郸丨邯山区AI... (来源:邯郸新闻网) 转自:邯郸新闻网 近日,在邯山区科技创新产业园内的邯郸人工智能教育基地,一拨拨...