Environment download
https://golang.google.cn/dl/, select the appropriate environment package and download it to the /usr/local directory
and decompress it
tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
Configure environment variables
- GOROOT is the installation directory of go
export GOROOT="/usr/local/go"
zversion.go: no such file or directory
使用了go1.6等低版本编译,安装新的go版本即可。
export GOROOT=/usr/lib/go
对于Ubuntu系统,默认使用Home/go目录作为gopath。
但是这里在编译过程中报错---go: open /usr/local/go/src/runtime/internal/sys/zversion.go: no such file or directory
经过百度之后发现,需要设置 /usr/lib/go 作为gopah,原因尚不清楚
该目录下有3个子目录:src,pkg,bin
- GOPATH , the location where the Go project code is stored. This is our own defined directory
export GOPATH=$HOME/go
- Gobin
export GOBIN=$GOROOT/bin
- Add GOBIN to GOPATH
export PATH=$PATH:$GOBIN
Let the configuration file take effect immediately
source $HOME/.profile
test installation
go version
go env
go project engineering
The gopath directory is where we store the source code we write. There are often 3 subdirectories under this directory: src, bin, pkg.
src —— Each subdirectory inside is a package. Inside the package is the source code file of Go
pkg —- generated after compilation, the target file of the package
bin —- Generated executable file.
first program
- Create a new src in the gopath directory, and create a new test file hello.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
- compile
$ go build hello.go
- run
$ ./hello
hello, world
uninstall
要从系统中删除现有的Go安装,请删除go目录。 在Linux,macOS和FreeBSD下通常为/ usr / local / go
还应该从PATH环境变量中删除Go bin目录。 在Linux和FreeBSD下,您应该编辑/ etc / profile或$ HOME / .profile。