【环境篇】golang环境变量二三事(一)

golang的环境变量有不少,平时安装完go之后,我们关注的一般只是GOPATH、GOROOT这些,还有与go mod有关的几个环境变量,对于其他变量了解不多,想要深入了解这门语言,有必要了解其他环境变量。

我们先总的来了解下有哪些环境变量,以及它们代表的含义:

$ 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 <NAME>, run ‘go env <NAME>‘. To change the default setting,
run ‘go env -w <NAME>=<VALUE>‘. 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

除开跟go mod有关的几个环境变量还有跟GOPATH有关的几个我们不谈,因为我已经在上篇文章讲go mod的时候聊过了,接下来就来了解一下其他的环境变量。

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的环境变量的第一阶段,还有一些环境变量等下篇文章再继续。



欢迎关注我的公众号:onepunchgo,会整理相关的文档和资料。

原文地址:https://blog.51cto.com/14664952/2467150

时间: 2024-11-02 20:14:12

【环境篇】golang环境变量二三事(一)的相关文章

【环境篇】golang环境变量二三事(三)

GCCGO The gccgo command to run for 'go build -compiler=gccgo'. 表示"go build -compiler=gccgo",编译时指定的-compiler参数. AR The command to use to manipulate library archives whenbuilding with the gccgo compiler.The default is 'ar'. 打包工具,默认"ar".

【环境篇】golang环境变量二三事(二)

继续上篇文章的学习,今天来看看其他的环境变量. GOEXE The executable file name suffix (".exe" on Windows, "" on other systems). 编译后的二进制文件后缀,在Windows平台是".exe",在其他平台是"". GOFLAGS 这个变量看起来比较陌生,用的不多,我们看看文档里是怎么解释的: A space-separated list of -flag

Ubuntu环境下golang环境搭建

一.更新国内镜像源 1. 网址:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 2. 备份Ubuntu默认源地址:sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup 3. 更换源服务器列表:sudo gedit /etc/apt/sources.list (已记事本的模式打开),然后将镜像内容清空并覆盖 4. 更新源 sudo apt-get update 二.环境配置 下载ht

【环境篇】搭建golang开发环境

学习一门语言,很多人都会是从搭建环境开始.有的语言的环境比较复杂,而有的语言环境很简单.对于go而言,说简单,的确不难,但有些概念需要先对其有所理解,才知道为什么这么用.网上对于怎么搭建一个golang开发环境已有了很多的教程,在这里我会简单介绍,不做过多描述.本篇文章仅对一些概念进行介绍,以及分享对一些我本人搭建开发环境的心得与技巧,如有错误,欢迎指正和交流. 搭建golang环境 1.到golang下载地址下载对应环境的安装包或者源码,若是Linux环境的话,将源码包下载解压之后,放至/us

CentOS6.5上golang环境配置

CentOS6.5上golang环境配置 一.下载和解压go环境包 >>cd /usr/local/src/ >>wget -c http://golangtc.com/static/go/go1.4beta1.linux-amd64.tar.gz >>tar zxvf go1.4beta1.linux-amd64.tar.gz -C /usr/local 二.设置系统环境变量 >>vi /etc/profile export GOROOT=/usr/loc

golang环境安装

一.下载go安装包 可以去官网下载https://golang.google.cn/dl/ ,也可以去go语言中文社区https://studygolang.com/dl下载,根据系统环境选择对应版本下载,linux环境下载后解压即可,windows环境下载根据安装包引导一步步安装即可 二.环境设置 linux环境 将go添加到Path环境变量中(go安装包解压到/usr/local目录下) vim /etc/profile 将:/usr/local/go/bin 添加到PATH全局变量中保存

Linux Golang 环境搭建(版本1.6.2)

1.下载安装包 https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz 2.解压缩安装包到/usr/local目录 tar -C /usr/local -xzf go1.6.2.linux-amd64.tar.gz 3.配置Golang环境变量 1) 配置到缺省位置:export PATH=$PATH:/usr/local/go/bin 2)配置到自定义目录($HOME): export GOROOT=$HOME/go e

项目构建之maven篇:1.环境搭建

maven下载: 下载地址 设置环境变量 查看安装成功 mvn -version 设置本地仓库 存放从中央仓库或私服中下载依赖包,自定义的项目使用install命令,安装到本地仓库中,供其它项目依赖 apache-maven-3.0.5\conf\settings.xml 项目构建之maven篇:1.环境搭建,布布扣,bubuko.com

android学习第一篇 开发环境搭建

android开发环境搭建 由于博主最近在学golang,所以就想着顺手把android开发给学了.最近这两天都在折腾idea,搭建开发环境.昨天晚上终于把golang和android的环境都搭好了,总结一下. 博主一开始使用android studio 做 android 开发,但使用idea做 golang 开发.这两个IDE都是功能十分庞大的,一同运行的话两个分别占用1.5G内存,所以就琢磨用idea开发安卓了.期间遇到几个坑,分享出来. 1.安装安卓SDK 下载地址 http://and