VSCode手动安装go插件的依赖

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
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在VSCode中手动安装件,你可以按照以下步骤进行操作: 1. 首先,你需要下载并解压件文件。你可以从件的官方网站或其他可信来源下载件的压缩文件(通常是一个`.vsix`文件)。 2. 打开VSCode,并点击左侧的扩展按钮(也可以使用快捷键`Ctrl+Shift+X`)。 3. 在扩展面板中,你可以直接搜索并安装件。在搜索栏中输入件的名称或关键词,然后点击安装按钮即可自动安装件[1]。 4. 如果无法在VSCode中搜索到所需件,你可以手动添加件。首先,解压下载的件文件,并得到一个件文件夹。 5. 找到VSCode的extensions文件夹。在Windows上,你可以在以下路径找到该文件夹:`C:\Users\你的用户名\.vscode\extensions`。 6. 将件文件夹复制到extensions文件夹中。 7. 重新启动VSCode,然后检查件是否已成功安装。你可以在扩展面板中找到已安装件,并根据需要进行配置和使用。 请记住,在安装件之前,确保件的版本与你正在使用的VSCode版本兼容。如果有旧版本的件,你可以先卸载旧版本,然后再安装新的版本。如果你遇到任何问题,也可以尝试删除以下文件夹:`C:\Users\你的用户名\AppData\Local\Programs\Microsoft VS Code`、`C:\Users\你的用户名\.vscode\extensions`和`C:\Users\你的用户名\AppData\Roaming\Code`,然后重新进行安装。 希望这些步骤对你有所帮助,祝你在VSCode中愉快地安装件!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值