Golang 环境搭建
马哥Linux运维
共 2556字,需浏览 6分钟
·
2021-10-16 15:21
安装Go
wget https://studygolang.com/dl/golang/go1.17.1.linux-amd64.tar.gz
没安装wget? [安装wget:yum install wget] OR [浏览器下载下来,用ftp上传上去]
$ tar -zxvf go1.17.1.linux-amd64.tar.gz -C /usr/local/
-C 是指定到目标目录, 一般情况我们用户安装的程序都指定到/usr/local/下
$ /usr/local/go/bin/go version
go version go1.17.1 linux/amd64
环境变量配置
我们先来看下安装完Go的当前环境变量是啥
** 查看命令 **
$ /usr/local/go/bin/go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go" ** go项目开发源码所在目录 **
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go" ** go源程序的安装目录 **
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.1"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2477519910=/tmp/go-build -gno-record-gcc-switches"
** 查看官方字段解释 **
$ /usr/local/go/bin/go help environment
** 写入系统加载配置 **
$ echo 'export GOROOT=/usr/local/go' >> /etc/profile
$ echo 'export GOPATH=$HOME/go' >> /etc/profile
$ echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> /etc/profile
** 加载刚写入的配置 **
$ source /etc/profile
go env -w key=value ** key如GOPATH、GOROOT ... **
官方解释(学英语的重要性🐶):
The -w flag requires one or more arguments of the
form NAME=VALUE and changes the default settings
of the named environment variables to the given values.
$ go env -w GOPATH=源代码所在目录 (我们这里默认每个用户目录下的go目录)
$ /usr/local/go/bin/go help gopath
$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct
文章转载:SegmentFault
(版权归原作者所有,侵删)
点击下方“阅读原文”查看更多
评论