VS Code 1.60.1配置并调试golang

1.安装Go 至C:\Program Files\Go下;安装VScode至任意位置;创建go项目所在文件夹,E:\go,并在该文件夹下创建bin、pkg、src文件夹;bin用于存储二进制可执行文件,pkg存储包文件,src存储项目源码文件;

2.配置环境变量GOPATH(go项目所在的文件夹)、GOROOT(go语言安装所在的文件夹);配置系统变量Path()

 3、启动vscode并安装go插件;

4、配置vscode中go的项目目录及go语言目录; 

 

5、设置网络代理;如果你用的是linux或mac,直接在terminal中运行go env -w GO111MODULE=on和go env -w GOPROXY=https://goproxy.cn,direct,如果你是windows,就是在环境变量里设置GO111MODULE=on和GOPROXY=https://goproxy.cn,direct

6、安装项目环境必要的包及依赖包;(使用vscode时可省略)

go get -u -v github.com/nsf/gocode
go get -u -v github.com/rogpeppe/godef
go get -u -v github.com/golang/lint/golint
go get -u -v github.com/lukehoban/go-find-references
go get -u -v github.com/lukehoban/go-outline
go get -u -v sourcegraph.com/sqs/goreturns
go get -u -v golang.org/x/tools/cmd/gorename
go get -u -v github.com/tpng/gopkgs
go get -u -v github.com/newhook/go-symbols
go get -u -v github.com/zmb3/gogetdoc
go get -u -v github.com/cweill/gotests
go get -u -v github.com/derekparker/delve/cmd/dlv
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v github.com/fatih/gomodifytags
go get -u -v github.com/haya14busa/goplay/cmd/goplay
go get -u -v github.com/josharian/impl
go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct
go get -u -v golang.org/x/tools/cmd/godoc
go get -u -v golang.org/x/tools/cmd/goimports

 

 7、vscode创建项目,编辑项目源文件,进行调试前安装必要的go调试插件;

安装创建后出现问题,提示如下内容:Failed to launch:Build error:Check the debug console for details.

Starting: E:\go\bin\dlv-dap.exe dap --listen=127.0.0.1:65147 from e:\go\src
DAP server listening at: 127.0.0.1:65147
Build Error: go build -o e:\go\src\__debug_bin.exe -gcflags all=-N -l .
go: go.mod file not found in current directory or any parent directory; see 'go help modules' (exit status 1)

 解决方法:

 go module是go官方自带的go依赖管理库,可将某个项目下的所有依赖整理成一个 go.mod 文件,该文件里面写入了依赖的版本等;使用 go module 管理依赖后会在项目根目录下生成两个文件 go.mod 和 go.sum;之后我们可不用将代码放置在src下。

windows环境下,cmd进入项目文件夹执行: go mod init example.com/greetings

 再次调试程序,出现错误:main()重复定义,一个project只能有1个main();

Starting: E:\go\bin\dlv-dap.exe dap --listen=127.0.0.1:54129 from e:\go\src
DAP server listening at: 127.0.0.1:54129
Build Error: go build -o e:\go\src\__debug_bin.exe -gcflags all=-N -l .
# example.com/greetings
.\datatype.go:17:6: main redeclared in this block
	e:\go\src\constant.go:8:6: previous declaration
.\flowControl.go:5:5: bool_0 redeclared in this block
	e:\go\src\datatype.go:5:5: previous declaration
.\flowControl.go:6:5: name redeclared in this block
	e:\go\src\datatype.go:6:5: previous declaration
.\flowControl.go:9:6: main redeclared in this block
	e:\go\src\datatype.go:17:6: previous declaration
.\function.go:19:6: main redeclared in this block
	e:\go\src\flowControl.go:9:6: previous declaration
.\structure.go:37:6: main redeclared in this block
	e:\go\src\function.go:19:6: previous declaration
.\test1.go:5:6: main redeclared in this block
	e:\go\src\structure.go:37:6: previous declaration
.\variable.go:5:5: name redeclared in this block
	e:\go\src\flowControl.go:6:5: previous declaration
.\variable.go:6:5: age redeclared in this block
	e:\go\src\datatype.go:7:5: previous declaration
.\variable.go:10:6: main redeclared in this block
	e:\go\src\test1.go:5:6: previous declaration
.\variable.go:10:6: too many errors (exit status 2)

 

 问题:无法使用vscode调试go程序?

Failed to run "go env" to find GOPATH as the "go" binary cannot be found in either GOROOT() or PATH(C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\)

原因分析:vscode配置未设置系统变量;

解决方法:vscode配置设置系统变量即可;

 

问题:vscode的终端无法使用go命令?

G:\go\src\main>go version
'go' 不是内部或外部命令,也不是可运行的程序或批处理文件。

原因分析:账号未设置特权等级。

解决方法:以管理员启动vscode。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

posted on 2021-09-17 09:55  chalon  阅读(1489)  评论(0编辑  收藏  举报