VSCode 安装 Go 插件失败解决方案

| 选择喜欢的代码风格  

VSCode 开始 Go 的项目:提示安装一些 Go 的插件,但是安装后,总提示报错,诸如:


The "go-outline" command is not available. Run "go get -v github.com/ramya-rao-a/go-outline"to install ..

这个时候,除了检查 GOPATHGOROOT 是否设置正确,运行下面命令检查:

$ go env

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Admin\AppData\Local\go-build
set GOENV=C:\Users\Admin\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Admin\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Admin\go
set GOPRIVATE=
set GOPROXY=
set GOROOT=E:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=E:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.4
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\Admin\AppData\Local\Temp\go-build404880772=/tmp/go-build -gno-record-gcc-switches

为了解决这个问题,可以使用下面 2 个命令,设置代理,其中用环境变量 GO111MODULE 开启或关闭模块支持,它有三个可选值:offonauto,默认值是 auto

  • GO111MODULE=off 无模块支持,go 会从 GOPATHvendor 文件夹寻找包。
  • GO111MODULE=on 模块支持,go 会忽略 GOPATHvendor 文件夹,只根据 go.mod 下载依赖。
  • GO111MODULE=auto$GOPATH/src 外面且根目录有 go.mod 文件时,开启模块支持。

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

再次运行命令 go env

$ go env

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Admin\AppData\Local\go-build
set GOENV=C:\Users\Admin\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Admin\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Admin\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=E:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=E:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.4
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\Admin\AppData\Local\Temp\go-build404880772=/tmp/go-build -gno-record-gcc-switches

然后再重新启动 VSCode,重新点击,安装插件,会提示 SUCCESS,如下提示:

Tools environment: GOPATH=C:\Users\Admin\go
Installing 10 tools at C:\Users\Admin\go\bin in module mode.
  gopkgs
  go-outline
  gotests
  gomodifytags
  impl
  goplay
  dlv
  dlv-dap
  staticcheck
  gopls

Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs (C:\Users\Admin\go\bin\gopkgs.exe) SUCCEEDED
Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs (C:\Users\Admin\go\bin\gopkgs.exe) SUCCEEDED
Installing github.com/ramya-rao-a/go-outline (C:\Users\Admin\go\bin\go-outline.exe) SUCCEEDED
Installing github.com/ramya-rao-a/go-outline (C:\Users\Admin\go\bin\go-outline.exe) SUCCEEDED
Installing github.com/cweill/gotests/gotests (C:\Users\Admin\go\bin\gotests.exe) SUCCEEDED
Installing github.com/cweill/gotests/gotests (C:\Users\Admin\go\bin\gotests.exe) SUCCEEDED
Installing github.com/fatih/gomodifytags (C:\Users\Admin\go\bin\gomodifytags.exe) SUCCEEDED
Installing github.com/fatih/gomodifytags (C:\Users\Admin\go\bin\gomodifytags.exe) SUCCEEDED
Installing github.com/josharian/impl (C:\Users\Admin\go\bin\impl.exe) SUCCEEDED
Installing github.com/josharian/impl (C:\Users\Admin\go\bin\impl.exe) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay (C:\Users\Admin\go\bin\goplay.exe) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay (C:\Users\Admin\go\bin\goplay.exe) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv (C:\Users\Admin\go\bin\dlv.exe) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv (C:\Users\Admin\go\bin\dlv.exe) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@master (C:\Users\Admin\go\bin\dlv-dap.exe) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@master (C:\Users\Admin\go\bin\dlv-dap.exe) SUCCEEDED
Installing honnef.co/go/tools/cmd/staticcheck (C:\Users\Admin\go\bin\staticcheck.exe) SUCCEEDED
Installing honnef.co/go/tools/cmd/staticcheck (C:\Users\Admin\go\bin\staticcheck.exe) SUCCEEDED
Installing golang.org/x/tools/gopls (C:\Users\Admin\go\bin\gopls.exe) SUCCEEDED

All tools successfully installed. You are ready to Go :).
Installing golang.org/x/tools/gopls (C:\Users\Admin\go\bin\gopls.exe) SUCCEEDED

All tools successfully installed. You are ready to Go :).

此时,再编辑 go 文件的时候,各种提示就可以完美支持了,如图:

go 扩展阅读:




发表评论