首页 > 其他 > 详细

VS Code 1.60.1配置并调试golang

时间:2021-09-17 16:22:05      阅读:78      评论:0      收藏:0      [点我收藏+]

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

技术分享图片

包下载问题:golint下载失败?

C:\Users\chalon>go get -u -v github.com/golang/lint/golint
go get: github.com/golang/lint@v0.0.0-20210508222113-6edffad5e616: parsing go.mod:
        module declares its path as: golang.org/x/lint
                but was required as: github.com/golang/lint

解决方法:

 

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

技术分享图片

 

技术分享图片

 

安装创建后出现问题,提示如下内容:

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)

技术分享图片

 

 

 

 

 

 

 

 

 

 

  

 

VS Code 1.60.1配置并调试golang

原文:https://www.cnblogs.com/chalon/p/15302262.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!