apt源下载
sudo apt install golang-go
go version
snap源下载
sudo snap install go
go version
官方安装包下载
官网下载地址:https://golang.google.cn/dl/
如下图所示,选择需要下载的版本(这里以1.12.5版本为例):
解压安装包
tar -zxvf go1.12.5.linux-amd64.tar.gz
设置环境变量
编辑profile文件
sudo vim /etc/profile
添加环境变量设置:
export PATH=$PATH:/home/lazycece/program-file/go/bin
查看结果
source /etc/profile
echo $PATH
第一个go程序
编写一个hello.go文件,内容如下:
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
用go run
命令执行go文件:
lazycece@cc-ubuntu:~/program-file$ go run hello.go
hello, word
或者用go build
命令生成可执行文件:
lazycece@cc-ubuntu:~/program-file$ go build hello.go
lazycece@cc-ubuntu:~/program-file$ ./hello
hello, word