2020-10-02 09:40:36 +08:00
|
|
|
# Linux 下搭建 Go 开发环境
|
|
|
|
|
|
|
|
## 1.安装和配置
|
|
|
|
|
|
|
|
### 1.1.安装
|
|
|
|
|
|
|
|
```bash
|
2021-06-29 16:52:00 +08:00
|
|
|
# Ubuntu
|
2022-04-29 13:20:16 +08:00
|
|
|
sudo apt install golang
|
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
|
|
|
```
|
|
|
|
|
|
|
|
### 1.2.配置
|
|
|
|
|
|
|
|
```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
|
|
|
# 安装扩展包
|
|
|
|
go get -v github.com/ramya-rao-a/go-outline
|
|
|
|
go install github.com/ramya-rao-a/go-outline
|
|
|
|
go get -v github.com/go-delve/delve/cmd/dlv
|
|
|
|
go install github.com/go-delve/delve/cmd/dlv
|
|
|
|
go get -v github.com/mdempsky/gocode
|
|
|
|
go install github.com/mdempsky/gocode
|
|
|
|
go get -v github.com/uudashr/gopkgs/v2/cmd/gopkgs
|
|
|
|
go install github.com/uudashr/gopkgs/v2/cmd/gopkgs
|
|
|
|
go get -v github.com/rogpeppe/godef
|
|
|
|
go install github.com/rogpeppe/godef
|
|
|
|
go get -v github.com/sqs/goreturns
|
2021-03-18 18:44:34 +08:00
|
|
|
go install github.com/sqs/goreturns
|
2021-07-16 09:09:35 +08:00
|
|
|
go get -v github.com/cweill/gotests/gotests
|
|
|
|
go install github.com/cweill/gotests/gotests
|
|
|
|
go get -v github.com/fatih/gomodifytags
|
|
|
|
go install github.com/fatih/gomodifytags
|
|
|
|
go get -v github.com/josharian/impl
|
|
|
|
go install github.com/josharian/impl
|
|
|
|
go get -v github.com/haya14busa/goplay/cmd/goplay
|
|
|
|
go install github.com/haya14busa/goplay/cmd/goplay
|
|
|
|
go get -v honnef.co/go/tools/cmd/staticcheck
|
|
|
|
go install honnef.co/go/tools/cmd/staticcheck
|
|
|
|
go get -v golang.org/x/tools/gopls
|
|
|
|
go install golang.org/x/tools/gopls
|
2021-07-27 16:45:27 +08:00
|
|
|
go get -v golang.org/x/tools/cmd/godoc
|
2022-06-20 13:21:48 +08:00
|
|
|
go install golang.org/x/tools/cmd/godoc
|
2020-10-02 09:40:36 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
### 1.3.VSCode 配置
|
|
|
|
|
|
|
|
修改 User Setting 的 go.gopath 为 $GOPATH。
|
|
|
|
|
|
|
|
## 2.第一个 Go 程序
|
|
|
|
|
|
|
|
```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!
|
|
|
|
```
|