从vscode中直接安装go相关插件大部分是失败的
go安装:
一、执行步骤
1、定位到如下目录(没有的话就创建)
cd $GOPATH/src/golang.org/x/
2、执行如下命令
git clone https://github.com/golang/tools.git (工具集以及包源代码)
git clone https://github.com/golang/lint.git (lint 大家都不陌生了)// godoctor 不再更新可以不考虑安装使用,可替代是 golint
git clone https://github.com/golang/mod.git (mod 模块支持)
git clone https://github.com/golang/xerrors.git (错误值转换包)
$GOPATH
d $GOPATH
sudo go install github.com/ramya-rao-a/go-outline@latest
sudo go install github.com/ramya-rao-a/go-outline@latest
sudo go install golang.org/x/tools/cmd/guru@latest // guru 和 gogetdoc 都是代码描述提示,建议使用 gogetdoc
sudo go install golang.org/x/tools/cmd/gorename@latest
sudo go install github.com/josharian/impl@latest
sudo go install github.com/rogpeppe/godef@latest
sudo go install github.com/sqs/goreturns@latest
sudo go install github.com/cweill/gotests/gotests@latest
sudo go install github.com/ramya-rao-a/go-outline@latest
sudo go install github.com/acroca/go-symbols@latest
sudo go install golang.org/x/tools/cmd/guru@latest
sudo go install golang.org/x/tools/cmd/gorename@latest
sudo go install github.com/josharian/impl@latest
sudo go install github.com/rogpeppe/godef@latest
sudo go install github.com/sqs/goreturns@latest // goimports 和 goreturns 是格式化和包引用管理,建议使用 goreturns
sudo go install github.com/golang/lint/golint@latest
sudo go install github.com/cweill/gotests/gotests@latest
安装完成后可以愉悦的使用vscode写go了。
installimportinstall
4.常用其他包
git clone https://github.com/golang/net.git (网络包)
git clone https://github.com/golang/sync.git (同步/原子包)
git clone https://github.com/golang/crypto.git (加密包)
git clone https://github.com/golang/debug.git (debug 工具包)
git clone https://github.com/golang/oauth2.git (oauth2 认证包)
git clone https://github.com/golang/protobuf.git (protobuf 协议包)
以上为额外的且常用的一些功能的源码扩展包,如有需要则可以考虑使用,当然也可以使用第三方实现的包。
GOPATH/src/github.com/go get
go get -u -v github.com/uudashr/gopkgs/cmd/gopkgs
go get -u -v github.com/ramya-rao-a/go-outline
go get -u -v github.com/rogpeppe/godef
go get -u -v github.com/godoctor/godoctor
go get -u -v github.com/acroca/go-symbols
go get -u -v github.com/fatih/gomodifytags
go get -u -v github.com/haya14busa/goplay/cmd/goplay
go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct
go get -u -v github.com/cweill/gotests/...(三个点不是错误,真的)
6. 非正常情况
go getclonego installGOPATH
go get -u -v github.com/sqs/goreturns (无法正常安装时使用以下命令)
git clone https://github.com/sqs/goreturns.git
go install github.com/sqs/goreturns.git
go get -u -v github.com/josharian/impl (无法正常安装时使用以下命令)
git clone https://github.com/josharian/impl.git
go install github.com/josharian/impl
gocode (核心 - 语法相关)
# go 语法与自动补全,gocode-gomod 需要使用 VS Code 提示安装完成。
https://github.com/stamblerre/gocode.git (支持模块)
go get -u -v github.com/stamblerre/gocode // 这个支持 gocode mod 的包有 Bug 牵引过多资源非常卡,占用大量 CPU 资源,不要安装使用!!
go modgopls
https://github.com/mdempsky/gocode.git (Go > 1.8 不支持模块)
go get -u -v github.com/mdempsky/gocode
https://github.com/nsf/gocode.git (旧版本,且不再维护)
go get -u -v github.com/nsf/gocode
二、VS Code 配置
文件 -> 首选项 -> 设置 -> settings.json
{
"go.buildOnSave": "workspace",
"go.lintOnSave": "package",
"go.vetOnSave": "workspace",
"go.coverOnSave": false,
"go.lintTool": "golint",
"go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
"go.useCodeSnippetsOnFunctionSuggest": true,
"go.autocompleteUnimportedPackages": true,
"go.gocodePackageLookupMode": "go",
"go.inferGopath": true,
"go.docsTool": "gogetdoc",
"go.formatTool": "goreturns",
}
GOPATH%/bin
go mod & gopls
go modGOPROXY
https://goproxy.cngoproxy.cnhttps://mirrors.aliyun.com/goproxy
配置 GOPROXY
配置方式 Windows
# Windows 1
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
# Windows 2
# 1. 打开并编辑系统环境变量
# 2. 新增 GOPROXY 变量名
# 3. 填写变量值 https://goproxy.cn
# 4. 确定并重启控制台
配置方式 macOS & Linux
# macOS & Linux 1
$ export GO111MODULE=on
$ export GOPROXY=https://goproxy.cn
# macOS & Linux 2
$ echo "export GO111MODULE=on" >> ~/.profile
$ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile
$ source ~/.profile
安装 gopls
GOPROXYgopls
手动安装
go get golang.org/x/tools/gopls@latest
自动安装
go mod init v2goplsinstall
以管理员身份安装vscode
mkdir vscode
dpkg -x vscode.deb vscode
./vscode/usr/share/code/code