英文:
VSCode wrong path while debugging Go program
问题
我在一个名为cmd/admin/main.go
的子文件夹中有我的main.go
文件,但是当我调试时,如果文件中有错误,它会给我返回相对于main.go
文件夹而不是工作区文件夹的路径。所以例如,我会得到错误信息..\..\path\to\file.go:238:3: undefined: test
,如果我尝试Ctrl+点击它是无法工作的。
如果我从根目录启动命令go run cmd/admin/main.go
,它会按预期返回path\to\file.go:238:3: undefined: test
。
我的launch.json
文件:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/cmd/admin",
"env": {},
"args": []
}
]
}
Go版本:1.16.6
VSC版本:1.58.2
操作系统:Windows10
英文:
I have my main.go in a subfolder cmd/admin/main.go
but when I'm debugging and there are errors in a file, it gives me the relative path from the main.go folder instead of the workspace folder. So for example I will have the error ..\..\path\to\file.go:238:3: undefined: test
which won't work if I try to Ctrl+click it.
If I launch the command from the root go run cmd/admin/main.go
that works as intended returning path\to\file.go:238:3: undefined: test
.
My launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/cmd/admin",
"env": {},
"args": []
}
]
}
Go version 1.16.6
VSC version 1.58.2
OS Windows10
答案1
得分: 9
打开文件菜单,选择“添加文件夹到工作区”,然后选择包含 main.go 文件的文件夹。你也可以在命令行中执行以下命令:code cmd/admin -a。确保删除当前的 launch.json 文件,使工作区恢复到初始状态。确保你的工作区看起来像这样:[图片1]。注意,代码中有一个 "package main" 和 "func main()",这是 Go 语言的入口点。现在按下运行和调试按钮,并设置断点:[图片2]。就是这样,现在它应该在你添加到工作区的任何文件夹中工作。如果你想要更多特定的调试选项,将它们添加到你的工作区,并在运行的文件上下文中应用。点击“创建 launch.json 文件”:[图片3]。选择工作区:[图片4]。选择 Go: Launch package:[图片5]。现在你有一个适用于你运行的目录的启动配置:[图片6]。记得保存你的工作区以保留它。注意事项:- 如果已经存在任何地方的 launch.json 文件,请确保删除它们。- 确保所有源代码都位于 GOPATH/src 中,你可以通过在命令行中输入以下命令来找到 GOPATH 的位置:go env GOPATH。- 文件的名称不需要是 main.go,但是你必须有一个名为 main 的包和一个名为 main 的函数,以便 Go 知道入口点,例如 influxdb 的两个 cli:https://github.com/influxdata/influxdb/tree/master/cmd。更多信息:VS Code 工作区调试:https://code.visualstudio.com/docs/editor/multi-root-workspaces#_debugging。GOPATH:https://golang.org/doc/gopath_code。
英文:
Go File > Add Folder to Workspace
Then select the folders containing main.go
You can also do it in the command line:
code cmd/admin -a
Now make sure your current launch.json has been deleted to start fresh, your workspace should look like this:
Notice that there is a 'package main' and 'func main()' this is required for Go to know the entry point.
Now press Run and Debug with a breakpoint:
That's it, it should now work on any folders you add to your workspace. If you want more specific debug options, add them to your workspace and they'll apply in the context of the file you run from. Click the 'create a launch.json file.:
Select workspace:
Select Go: Launch package
You now have a launch config that will apply to the directory you run it from:
Make sure to save your workspace to keep it:
Notes
-
Be sure to delete your current launch.json files if they already exist anywhere.
-
Make sure all of your source code is located in GOPATH/src, you can find out where GOPATH is by putting this into a command line:
go env GOPATH
- The file doesn't need to be named main.go, but you MUST have a package named main, and a func named main for Go to know the entry point, for each executable, for example influxdb's two cli's: https://github.com/influxdata/influxdb/tree/master/cmd
More info
VS Code workspace debugging:
https://code.visualstudio.com/docs/editor/multi-root-workspaces#_debugging
答案2
得分: 1
请确保以下事项:
- 检查是否设置了任何
GOxxx
环境变量(如GO_BASE_PATH
、GOROOT
),除了将GOPATH
设置为%USERPROFILE%\go
,并将%GOPATH%\bin
添加到%PATH%
中。 - 确保你正在使用默认安装在
C:\Program Files\Go\
文件夹中的Go。 - 使用Go模块设置你的项目,使用命令
go mod init myproject
。 - 为你的项目根文件夹定义一个多根工作区,只包含一个根:你的项目。保存该工作区(它将创建一个
<name>.code-workspace
的JSON文件)。
然后查看问题是否仍然存在(在你的launch.json
中不应该需要cwd
)。
英文:
Try and make sure:
- you have no
GOxxx
environment variable set (noGO_BASE_PATH
, noGOROOT
), except forGOPATH
set to%USERPROFILE%\go
, and%GOPATH%\bin
in your%PATH%
- you are using Go installed in the default
C:\Program Files\Go\
folder - you have set up your project using Go modules, with a
go mod init myproject
- you have defined a multi-root workspace for your project root folder, compose of only one root: your project. Save that workspace (it will create a
<name>.code-workspace
JSON file).
See then if the issue persists (and no cwd
should be needed in your launch.json
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论