https://studygolang.com/dl
# rm -rf /usr/local/go && tar -C /usr/local -xzf go1.16.6.linux-amd64.tar.gz
添加如下信息
export PATH=$PATH:/usr/local/go/bin
# source /etc/profile
# 检查PATH环境变量
# echo $PATH
/usr/lib64/qt-3.3/bin:/root/perl5/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/go/bin
# 检查Go版本信息
# go version
go version go1.16.6 linux/amd64
主要关注三个Go环境变量,GOROOT和 GOPATH,以及GO111MODULE
GOROOT
是Golang源代码目录,安装目录;
GOPATH是Golang项目代码目录
可以通过以下命令改写:
go env -w GOROOT="/usr/local/go
"
go env -w GOPATH="/home/goproject
"
go env -w GO111MODULE="off
" 或者 "auto"
否则编译自己的工程目录时,出现查找失败问题:
# go build helloworld
package wintcpcore is not in GOROOT (/usr/local/go/src/helloworld)
[root@localhost ~]# go env
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/goproject/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/goproject"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.6"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build2323615239=/tmp/go-build
-gno-record-gcc-switches"
# mkdir /home/goproject
# mkdir /home/goproject/bin
# mkdir /home/goproject/src
# mkdir /home/goproject/pkg
# ls goproject/
bin pkg src
目录说明:
src:存放go源代码的目录,存放Golang项目的目录,以及所有项目代码都放在这个目录下
bin:在go install后生成的可执行文件的目录
pkg:编译后生成的,源码文件,如.a
(本文作者实践的例子中,并没有文件在这个目录下,需要再继续研究)
以创建HelloWorld项目为例:)
# ll
total 7040
drwxr-x--- 2 root root 4096 Jul 28 14:24 bin
drwxr-x--- 2 root root 4096 Jul 28 14:24 pkg
drwxr-x--- 3 root root 4096 Jul 28 14:24 src
# ll /home/goproject/src
total 12
drwxr-x--- 2 root root 4096 Aug 2 19:49 helloworld
# ll /home/goproject/src/helloworld/
total 4
-rw-r----- 1 root root 443 Aug 2 19:49 helloworld.go
[root@localhost goproject]# go install helloworld
[root@localhost goproject]# ll bin/
total 1900
-rwxr-x--- 1 root root 1938248 Aug 3 20:46 helloworld
# 运行helloworld
# ./bin/helloworld
Hello, World!
参考资料:
【1】Goland创建Go project 配置当前project GOPATH
https://blog.csdn.net/qq_33398607/article/details/103820841?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.control&spm=1001.2101.3001.4242
【2】Go 安装包下载
https://studygolang.com/dl
【3】下载和安装Golang软件包
http://docs.studygolang.com/doc/install
【4】Go 模块解惑:到处都是 GO111MODULE ,这到底什么?
https://learnku.com/go/t/39086
【5】Linux搭建Go环境
https://www.cnblogs.com/anthony-wang0228/articles/11089172.html
【6】初学者没有搞明白的GOROOT,GOPATH,GOBIN,project目录
https://www.cnblogs.com/zhaof/p/7906722.html