2020-10-02 09:40:36 +08:00
|
|
|
# Linux 下搭建 Go 开发环境
|
|
|
|
|
2023-04-07 17:08:06 +08:00
|
|
|
## 1. 安装和配置
|
2020-10-02 09:40:36 +08:00
|
|
|
|
2023-04-07 17:08:06 +08:00
|
|
|
### 1.1. 安装
|
2020-10-02 09:40:36 +08:00
|
|
|
|
|
|
|
```bash
|
2023-06-28 12:18:53 +08:00
|
|
|
# Ubuntu Snap (版本新)
|
|
|
|
snap install go --classic
|
|
|
|
# Ubuntu (版本旧)
|
2022-04-29 13:20:16 +08:00
|
|
|
sudo apt install golang
|
2023-06-28 12:18:53 +08:00
|
|
|
|
2020-10-02 09:40:36 +08:00
|
|
|
# Manjaro
|
2022-04-29 13:20:16 +08:00
|
|
|
sudo pacman -S go
|
2020-10-02 09:40:36 +08:00
|
|
|
```
|
|
|
|
|
2023-04-07 17:08:06 +08:00
|
|
|
### 1.2. 配置
|
2020-10-02 09:40:36 +08:00
|
|
|
|
|
|
|
```bash
|
|
|
|
# 配置环境,将 /Path/to/Go 替换为自己实际的 go 目录
|
|
|
|
echo "export GOPATH=/Path/to/Go" >> ~/.bashrc
|
|
|
|
echo "export GOBIN=$GOPATH/bin" >> ~/.bashrc
|
|
|
|
echo "export PATH=$GOBIN:$PATH" >> ~/.bashrc
|
|
|
|
# 可运行 go env 查看gol环境变量
|
|
|
|
go env
|
2021-06-29 16:52:00 +08:00
|
|
|
# 开启 GO111MODULE 并设置代理
|
|
|
|
go env -w GO111MODULE=on
|
|
|
|
go env -w GOPROXY=https://goproxy.cn
|
2020-10-02 09:40:36 +08:00
|
|
|
# 安装基础包
|
|
|
|
go get -v github.com/golang/tools
|
2021-07-16 08:46:26 +08:00
|
|
|
ln -s $GOPATH/src/github.com/golang/tools $GOPATH/src/golang.org/x/tools
|
2020-10-02 09:40:36 +08:00
|
|
|
# 安装扩展包
|
2023-03-07 08:23:42 +08:00
|
|
|
go install github.com/ramya-rao-a/go-outline@latest
|
|
|
|
go install github.com/go-delve/delve/cmd/dlv@latest
|
|
|
|
go install github.com/mdempsky/gocode@latest
|
|
|
|
go install github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest
|
|
|
|
go install github.com/rogpeppe/godef@latest
|
|
|
|
go install github.com/sqs/goreturns@latest
|
|
|
|
go install github.com/cweill/gotests/gotests@latest
|
|
|
|
go install github.com/fatih/gomodifytags@latest
|
|
|
|
go install github.com/josharian/impl@latest
|
|
|
|
go install github.com/haya14busa/goplay/cmd/goplay@latest
|
|
|
|
go install honnef.co/go/tools/cmd/staticcheck@latest
|
|
|
|
go install golang.org/x/tools/gopls@latest
|
|
|
|
go install golang.org/x/tools/cmd/godoc@latest
|
2023-10-23 14:07:54 +08:00
|
|
|
go install golang.org/x/tools/cmd/goimports@latest
|
2020-10-02 09:40:36 +08:00
|
|
|
```
|
|
|
|
|
2023-04-07 17:08:06 +08:00
|
|
|
### 1.3. VSCode 配置
|
2020-10-02 09:40:36 +08:00
|
|
|
|
|
|
|
修改 User Setting 的 go.gopath 为 $GOPATH。
|
|
|
|
|
2023-04-07 17:08:06 +08:00
|
|
|
### 1.4. 移除不使用的包
|
|
|
|
|
|
|
|
到 $GOPATH/pkg 下删除对应的源码,然后使用:
|
|
|
|
|
|
|
|
```bash
|
2023-04-07 17:10:21 +08:00
|
|
|
go clean -i -r
|
2023-04-07 17:08:06 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
刷新一下即可。
|
|
|
|
|
|
|
|
## 2. 第一个 Go 程序
|
2020-10-02 09:40:36 +08:00
|
|
|
|
|
|
|
```go
|
|
|
|
/**
|
|
|
|
* @file hello.go
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("Hello, World!")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
测试和编译方法如下:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ go run hello.go
|
|
|
|
Hello, World!
|
|
|
|
|
|
|
|
$ go build hello.go
|
|
|
|
$ ls
|
|
|
|
hello hello.go
|
|
|
|
|
|
|
|
$ ./hello
|
|
|
|
Hello, World!
|
|
|
|
```
|
2023-04-07 17:10:21 +08:00
|
|
|
|
2023-10-23 14:07:54 +08:00
|
|
|
## 3. 常用 Go 包
|
2023-06-28 13:04:56 +08:00
|
|
|
|
2023-10-23 14:07:54 +08:00
|
|
|
```bash
|
|
|
|
go install github.com/cjbassi/gotop@latest
|
|
|
|
```
|
|
|
|
|
|
|
|
## 4. 常见问题
|
|
|
|
|
|
|
|
### 4.1. Snap 安装后 VSCode Go 插件报 Failed to run '/snap/bin/go env' error
|
2023-06-28 13:04:56 +08:00
|
|
|
|
|
|
|
进入 VSCode 用户配置(JSON),添加:
|
|
|
|
|
|
|
|
```json
|
|
|
|
"go.alternateTools": {
|
|
|
|
"go": "/snap/go/current/bin/go"
|
|
|
|
},
|
|
|
|
```
|
|
|
|
|
2023-10-23 14:07:54 +08:00
|
|
|
## 5. 外部参考资料
|
2023-04-07 17:10:21 +08:00
|
|
|
|
|
|
|
1. [How to remove package installed with go get](https://www.golinuxcloud.com/remove-installed-package-with-go-get/)
|