VS code中手动安装go的插件

Visual Studio Code是一个轻量且功能非常强大的代码开发工具; 支持Windows、MacOS和Linux三种系统的桌面应用; 虽然它一开始主要设计为JavaScript, TypeScript and Node.js,但是它支持通过丰富的插件来支持其他更多的开发语言比如C++, C#, Java, Python, PHP, Go, .NET and Unity等

VS开发Go程序

安装Go环境

  1. 下载go sdk
  2. 安装环境 install
  3. 验证一下, 重新打开一个新的终端
go version
go version go1.13.3 darwin/amd64

VS中安装go插件

  1. 在VS的extension管理中搜索’Go’, 查看插件介绍, 点击安装, 安装成功后重启VS;
  2. 新建一个Go程序文件, 文件后缀为.go, 写几行简单的代码;
package main
import "fmt"
func main() {
    fmt.Println("Google" + "Runoob")
    f
}
  1. 在上面代码的过程中, 强大的VS会检测并推荐安装一些额外的插件如下, 根据相关动作触发提示, 比如代码补全会提示gocode, 进行程序debug会提示安装dlv等;
gocode for auto-completion (not needed if using language server)
go-outline for symbol search in the current file
go-symbols for symbol search in the current workspace
gopkgs for auto-completion of unimported packages
guru for the Find all References feature
gorename for renaming symbols
goreturns or goimports for formatting code (not needed if using language server)
godef or gogetdoc for the Go to Definition feature (not needed if using language server)
godoc or gogetdoc for the documentation that appears on hover (not needed if using language server)
golint or megacheck or golangci-lint or revive for linting
dlv for debugging

关于这些插件的详情, 可以在我们一开始安装的Go插件详情介绍里面找到相关说明和链接;

  1. 本来我们可以通过一键点击安装各种强大好玩的插件, 但是由于网络问题全部都会安装失败;
Installing 15 tools at /Users/hinsteny/go/bin in module mode.
  gocode
  gopkgs
  go-symbols
  guru
  gorename
  gotests
  gomodifytags
  impl
  fillstruct
  goplay
  godoctor
  gocode-gomod
  godef
  goreturns
  golint

Installing github.com/mdempsky/gocode FAILED
Installing github.com/uudashr/gopkgs/cmd/gopkgs FAILED
Installing github.com/acroca/go-symbols FAILED
Installing golang.org/x/tools/cmd/guru FAILED
Installing golang.org/x/tools/cmd/gorename FAILED
Installing github.com/cweill/gotests/... FAILED
Installing github.com/fatih/gomodifytags FAILED
Installing github.com/josharian/impl FAILED
Installing github.com/davidrjenni/reftools/cmd/fillstruct FAILED
Installing github.com/haya14busa/goplay/cmd/goplay FAILED
Installing github.com/godoctor/godoctor FAILED
Installing github.com/stamblerre/gocode FAILED
Installing github.com/rogpeppe/godef FAILED
Installing github.com/sqs/goreturns FAILED
Installing golang.org/x/lint/golint FAILED

15 tools failed to install.

gocode:
Error: Command failed: /usr/local/go/bin/go get -v github.com/mdempsky/gocode
go get github.com/mdempsky/gocode: module github.com/mdempsky/gocode: Get https://proxy.golang.org/github.com/mdempsky/gocode/@v/list: dial tcp 172.217.24.17:443: i/o timeout
go get github.com/mdempsky/gocode: module github.com/mdempsky/gocode: Get https://proxy.golang.org/github.com/mdempsky/gocode/@v/list: dial tcp 172.217.24.17:443: i/o timeout
......

通过命令行手动安装Go插件

  1. 进入到我们操作系统的GOPATH目录,
    查看GOPATH
go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/hinsteny/Library/Caches/go-build"
GOENV="/Users/hinsteny/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/hinsteny/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
......

进入GOPATH的src目录下

cd /Users/hinsteny/go
cd src
  1. 这里以手动安装gocode(代码补全)插件为例

a. 可以在上面一键安装失败的输出信息中看到gocode源码所在地址,

go get github.com/mdempsky/gocode: module github.com/mdempsky/gocode: Get https://proxy.golang.org/github.com/mdempsky/gocode/@v/list: dial tcp 172.217.24.17:443: i/o timeout

b. 去github上看一下gocode插件项目

c. 在src目录下创建子目录 github.com/mdempsky, 然后再将上面的项目clone至其下面

git clone [email protected]:go-delve/delve.git

d. 最后就是通过 go install 命令安装我们下载到本地的插件

go install github.com/mdempsky/gocode

如果在执行install命令的时候报错找不到其他一些类时, 那就以同样的方式先去安装依赖的插件;

愉快的使用插件

  1. 使用gocode插件的代码自动提示功能
    Code
  2. 使用dle进行debug
    Debug
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章