VSCode手动安装go插件的依赖

tech2022-12-09  109

vscode安装了Go插件之后,使用各种功能时,需要用到go语言的依赖包。自动安装会报错,这里介绍手动安装方法。 介绍一个例子:安装goreturns依赖(自动格式化代码的工具)。

先配置环境变量GOPATH,我的配置是 E:\go

$ echo $GOPATH E:\go

按照github的说明,执行安装命令:(会报错获取依赖失败,后面手动处理)

$ go get -u github.com/sqs/goreturns package golang.org/x/tools/imports: unrecognized import path "golang.org/x/tools/imports": https fetch: Get "https://golang.org/x/tools/imports?go-get=1": dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

尝试直接安装,报错:(此步骤可省略)

$ go install $GOPATH/src/github.com/sqs/goreturns sqs\goreturns\goreturns.go:21:2: cannot find package "golang.org/x/tools/imports" in any of: c:\go\src\golang.org\x\tools\imports (from $GOROOT) E:\go\src\golang.org\x\tools\imports (from $GOPATH)

意思是goreturns依赖tools包,于是安装tools工具: 按照github的md文件说明,此包需要安装到路径$GOPATH/src/golang.org/x/tools下

$ git clone https://github.com/golang/tools.git $GOPATH/src/golang.org/x/tools

然后再安装,还是报错:(此步骤可省略)

$ go install $GOPATH/src/github.com/sqs/goreturns ..\golang.org\x\tools\internal\imports\mod.go:17:2: cannot find package "golang.org/x/mod/module" in any of: c:\go\src\golang.org\x\mod\module (from $GOROOT) E:\go\src\golang.org\x\mod\module (from $GOPATH) ..\golang.org\x\tools\internal\gocommand\vendor.go:16:2: cannot find package "golang.org/x/mod/semver" in any of: c:\go\src\golang.org\x\mod\semver (from $GOROOT) E:\go\src\golang.org\x\mod\semver (from $GOPATH)

意思是tools包依赖mod包,于是安装mod包:

$ git clone https://github.com/golang/mod.git $GOPATH/src/golang.org/x/mod

然后再安装,仍然报错:(此步骤可省略)

$ go install $GOPATH/src/github.com/sqs/goreturns golang.org\x\mod\module\module.go:107:2: cannot find package "golang.org/x/xerrors" in any of: c:\go\src\golang.org\x\xerrors (from $GOROOT) E:\go\src\golang.org\x\xerrors (from $GOPATH)

意思是mod包又依赖xerrors包,继续满足它:

$ git clone https://github.com/golang/xerrors.git $GOPATH/src/golang.org/x/xerrors

最后,终于没报错了:

$ go install $GOPATH/src/github.com/sqs/goreturns

然后测试一下,在.go文件中格式化代码(alt+shift+f),我这边生效了,你呢?

这里在网上收集了一些常用的工具和依赖包,我们可以在一开始就全下好准备着,省的一步步的操作。

常用依赖:

$ cd $GOPATH/src/golang.org/x/ git clone https://github.com/golang/tools.git git clone https://github.com/golang/lint.git git clone https://github.com/golang/mod.git git clone https://github.com/golang/xerrors.git git clone https://github.com/golang/net.git

常用工具:

gocode 代码自动补全 https://github.com/mdempsky/gocode go-outline 在当前文件中查找 https://github.com/ramya-rao-a/go-outline go-symbols 在项目路径下查找 https://github.com/acroca/go-symbols gopkgs 自动补全未导入包 https://github.com/uudashr/gopkgs guru 查询所有引用 https://golang.org/x/tools/cmd/guru gorename 重命名符号 https://golang.org/x/tools/cmd/gorename goreturns 格式化代码 https://github.com/sqs/goreturns godef 跳转到声明 https://github.com/rogpeppe/godef godoc 鼠标悬浮时文档提示 https://golang.org/x/tools/cmd/godoc golint 就是lint https://golang.org/x/lint/golint dlv 调试功能 https://github.com/derekparker/delve/tree/master/cmd/dlv gomodifytags 修改结构体标签 https://github.com/fatih/gomodifytags goplay 运行当前go文件 https://github.com/haya14busa/goplay/ impl 新建接口 https://github.com/josharian/impl gotype-live 类型诊断 https://github.com/tylerb/gotype-live gotests 单元测试 https://github.com/cweill/gotests/ go-langserver 语言服务 https://github.com/sourcegraph/go-langserver filstruct 结构体成员默认值 https://github.com/davidrjenni/reftools/tree/master/cmd/fillstruct
最新回复(0)