软件包下载地址:
https://golang.org/dl/
https://golang.google.cn/dl/
选择linux版下载
把包下载下来,解压到/usr/local/下
tar -C /usr/local -zxvf go1.10.3…….
然后在桌面上创建一个目录
sparrow@captain:~/桌面$ mkdir Golang
sparrow@captain:~/桌面/Golang$ mkdir src pkg bin
sparrow@captain:~/桌面/Golang$ ls
bin pkg src
sparrow@captain:~/桌面/Golang$ sudo vim ~/.bashrc
在文件最后添加
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/home/sparrow/桌面/Golang
路径请根据实际情况修改。
sparrow@captain:~/桌面/Golang$ source ~/.bashrc
sparrow@captain:~/桌面/Golang$ go ##测试是否生效
出现以下结果
Go is a tool for managing Go source code.
Usage:
go command [arguments]
The commands are:
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
bug start a bug report
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
Use “go help [command]” for more information about a command.
Additional help topics:
c calling between Go and C
buildmode build modes
cache build and test caching
filetype file types
gopath GOPATH environment variable
environment environment variables
importpath import path syntax
packages package lists
testflag testing flags
testfunc testing functions
Use “go help [topic]” for more information about that topic.
sparrow@captain:~/桌面/Golang$ go version //查看go版本
sparrow@captain:~/桌面/Golang$ cd src/
sparrow@captain:~/桌面/Golang/src$ ls
sparrow@captain:~/桌面/Golang/src$ mkdir hello
sparrow@captain:~/桌面/Golang/src$ cd hello/
sparrow@captain:~/桌面/Golang/src/hello$ touch hello.go
sparrow@captain:~/桌面/Golang/src/hello$ vim hello.go
内容如下,向世界问好。
package main
import “fmt”
func main() {
fmt.Println(“hello world!”)
}
sparrow@captain:~/桌面/Golang/src/hello$ go install hello //编译hello.go
sparrow@captain:~/桌面/Golang/src$ cd ../bin
sparrow@captain:~/桌面/Golang/bin$ ./hello
hello world!
或者直接使用go run 命令执行代码。
sparrow@captain:~/桌面/Golang/src/hello$ go run hello
hello world!