2021年12月14日13:53:12 星期二
1. linux 搭建 go 编程环境
a. 下载 go 开发程序
curl -O https://go.dev/dl/go1.17.5.linux-amd64.tar.gz
tar xf go1.17.5.linux-amd64.tar.gz
sudo mv go /usr/local
b.配置环境变量
在 /etc/profile 增加 go 环境变量设置
sudo vim /etc/profile
#add go env
export GOROOT=/usr/local/go
export GOPATH=/home/apple/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
查看 go version
# source /etc/profile
# go version
go version go1.17.5 linux/amd64
c. 安装 go module
因为有的时候,安装 go 模块,会连不上网络,所以要设置网络路径代理为cn
安装 go-outline 模块
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
go env -w GOPROXY=https://goproxy.cn,https://gocenter.io,https://goproxy.io,direct
go install github.com/rmya-rao-a/go-outline@latest
安装完,模块可执行文件在 $GOPATH/bin/ 目录下面,
查看安装结果:
# go env|grep GOPATH
GOPATH="/home/apple/go"
# ls ~/go/
bin pkg
#ls -l ~/go/bin/go-outline
-rwxrwxr-x 1 apple apple 3192083 12月 13 12:00 /home/apple/go/bin/go-outline
2. vsode 配置 go extension
go extension如果不能联网,可以下载离线安装。
wget https://github.com/766b/vscode-go-outliner/releases/download/v0.1.19/766b.go-outliner-0.1.19.vsix
install from VSIX...
第一个程序 hello.go
vscode hello.go源代码
//hello.go
package main
import (
"fmt"
)
func main() {
fmt.Println("hello go build")
}
初始化 mod 文件
# ls
hello.go
# go mod init hello
go: creating new go.mod: module hello
go: to add module requirements and sums:
go mod tidy
go.mod 内容
go.mod
# cat -n go.mod
1 module hello
2
3 go 1.17
build 并 install
# go build
# ls
go.mod hello hello.go
# go install
# ls
go.mod hello.go
# ls ~/go/bin/hello -l
-rwxrwxr-x 1 apple apple 1766438 12月 14 14:43 /home/apple/go/bin/hello
直接运行 hello 执行程序
# hello
hello go build
网友评论