英文:
How to set up debugging in GoLand?
问题
我正在尝试使用GoLand,并听说它的调试器非常棒,但我找不到任何解释如何设置它的文档(GoLand目前处于预览阶段,所以这并不奇怪)。我的使用情况是:我正在编写一个REST API,我使用curl发送请求。有人可以告诉我如何逐步开始使用调试器吗?
[编辑1](从评论中移动)当我点击运行/调试(或运行/运行)时,会显示一个窗口,要求我编辑配置。我可以选择几种不同类型的配置,我尝试选择“Go应用程序”,但调试按钮从未变灰,我无法使其可点击。我对GoLand非常陌生,可能是我漏掉了一些明显的设置,但我找不到缺少的设置在哪里。
[编辑2] 原来我的工作空间不符合Go的规范:我的代码不在名为src的目录中。现在它符合规范了,我可以点击运行/调试,GoLand似乎在做正确的事情,但它不会在断点处停止,也不会像调试器一样运行我的代码。
[编辑3] 再次尝试,它可以工作了。有点。我不知道为什么现在它可以工作,尽管之前不能工作,而且我没有做任何更改。现在断点在某些行上工作,而在其他行上不工作。或者调试器在断点处停止,但我永远等待变量列表加载。嗯,毕竟这只是一个预览版本...
[编辑4] 我刚刚收到通知说有一个新版本的GoLand可用。我升级了,现在调试工作得很好。
英文:
I am trying out GoLand and hear that the debugger is supposedly awesome, but I can’t find any documentation explaining how to set it up (GoLand is in preview stage, so that’s not really surprising). My use case: I am writing a REST API to which I send requests using curl. Could someone please tell me, step by step, how to get started with the debugger ?
[edit 1] (moved from comment) When I click Run/Debug (or Run/Run), a window is displayed, asking me to edit configurations. I am given a choice of several kinds of configurations, I try to choose "Go application" but the Debug button never is grayed and I can't get it to become clickable. I am very new to GoLand, I may be missing something obvious, but I can’t find whatever settings are missing.
[edit 2] It turns out that my workspace did not conform to the Go specifications : my code was not in a directory named src. Now that it is the case, I can click Run/Debug and GoLand seems to be doing the right thing, except it doesn’t stop at breakpoints or otherwise behave like a debugger, my code just runs.
[edit 3] Tried again, and it works. Sort of. I have no idea why it works now, even though it didn’t previously and I didn’t change anything. Now breakpoints work at some lines, and not at others. Or the debugger stops at the breakpoint, but I wait forever for the list of variables to be loaded. Well, it is a preview, after all...
[edit 4] I was notified just today that a new version of GoLand was available. I upgraded, and debugging is working well for me now.
答案1
得分: 12
GoLand如果你的代码不在src
目录下,将不会启用调试操作(尽管运行操作可用)。关于此问题没有任何警告,所以解决这个问题可能会非常困难。
此外,根据你的项目结构,调试可能仍然无法正常工作。在这种情况下,删除所有运行配置并创建一个新的配置可能会有所帮助。关于这种行为,有一个错误报告。
PS:尽管OP在问题的编辑中提到了第一部分,但我认为在回答中提到这一点是值得的。
英文:
GoLand will not enable the debug action (although run is available) if your code does not reside inside src
. There is no warning about this, so it might be really a pain to solve this.
Additionally, depending on your project’s structure it might be possible, that debugging is still not working. In that case, it might help to remove all run configurations and create a new one. There’s is a bug report about this behavior.
PS: Although the OP mentions the first part in an edit of the question, but it think it’s worth mentioning in an answer.
答案2
得分: 8
@Jamillo Santos回复后,Goland发生了一些变化。
首先,不再有"Go Application"。
最简单的方法是找到包含main()函数的文件。
应该有一个小三角形指向它,就像附图中所示:
当你点击小绿色三角形时,会弹出一个菜单,其中包含运行/调试选项。
每个选项都会创建一个go build
配置。
你可以编辑go build
配置,并为你的应用程序添加程序参数。
例如,如果你的程序有一个命令行参数--arg=value
,你可以像这样添加:
英文:
Things changed in goland since @Jamillo Santos added his reply.
To start with, there's no more "Go Application".
The easiest way is to locate the file containing the main() function.
There should be a small triangle pointing to it, like in the attached screenshot:
When you click on the small green triangle, you get a menu with Run/Debug options.
Each will create a go build
configuration.
You can edit that go build
configuration and add the program arguments for your application.
For example, if your program have a command line argument of --arg=value
you can add it like this:
答案3
得分: 6
我遇到了同样的问题,并通过使用“Go Application”配置(在运行/调试配置窗口中)解决了这个问题,而不是使用“Go Single File”配置。
所以,去Gogland的右上角点击“Edit configurations”。
然后,使用“Go Application”配置添加一个新的配置。
保存后,你应该能够调试你的代码。
启用这个配置的简单方法是使用@user1793301的方法,右键单击要运行的文件,然后选择“Debug 'go run
可能的解释:
注意:我没有进一步了解这个问题的详细信息。
事实:在运行之前,“Go Application”配置会构建可执行的二进制文件。
事实:通过查看控制台输出(在Goglang内部),我发现IDE使用DLV作为调试工具。
事实:我在DLV文档中找到了dlv debug
,它似乎可以编译和调试代码。
假设:Jetbrains的开发人员可能没有实现这个功能。或者至少目前还没有实现。
英文:
I was facing this same issue and I solved by using the "Go Application" configuration (at the Run/Debug Configurations window), instead of the "Go Single File" configuration.
So, go to the upper right corner of the Gogland and "Edit configurations".
Then, add a new configuration using the "Go Application" profile.
After saving, you should be able to debug your code. o/
An easy way to enable this configuration is to use the @user1793301 method and right click on the file you want to run and select "Debug 'go run <your file>.go'".
POSSIBLE EXPLANATION
NOTICE: I did not look any further details about it.
Fact: "Go Application" configuration does build the binary executable before running it.
Fact: I could see looking at the console output (inside of the Goglang) is that the IDE uses the DLV as debugging tool.
Fact: I found a dlv debug
at the DLV documentation and it seems to compile and debug the code.
Hypothesis: The guys from Jetbrains did not implement it. Or at least not YET.
答案4
得分: 4
在版本2019.1.1
中,不再有Go Application。请改用Go Build。在这里,我正在调试一个单独的脚本文件,但如果你有一个服务器应用程序或其他东西,只需指向主应用程序文件即可:
请注意,在Files字段中,您必须指定实际的文件路径。如果您想要有更多的起始点,可以使用管道符|
将它们分隔开。对于每个您想要调试的文件,您需要指定完整路径。
同样,如果您正在调试一个具有单个起始点的服务器应用程序或其他内容,您只需要在配置中指定启动文件。
英文:
On version 2019.1.1
there's no more Go Application. Use Go Build instead. Here I am debugging a single script file, but if you have a server app or smth, just point to the main app file:
Notice that in the Files field you have to specify the actual file path. You can put more files (if you want to have more starting points) by separating them with a pipe |
. For each file you want to be able to debug, you need to specify the full path.
Again, if you are debugging a server app or something else, that has a single starting point, you only need the startup file specified in the config.
答案5
得分: 1
我遇到了完全相同的问题。
删除并重新创建了调试运行配置。然后调试就完美地工作了。
英文:
I had this exact problem.
Deleted and recreated the Debug run configuration. Debugging then worked perfectly.
答案6
得分: 1
我按照以下步骤启动我的 Golang 服务器以进行调试(你可能想使用其他方法来启动服务器):
- 创建一个 makefile 文件。
- 在文件中定义你的服务器/工作进程的启动命令。
- 通过创建一个新的
Makefile
配置来运行它。 - 转到
Run -> Attach to process -> 选择你正在运行的服务器
。
这将把调试器附加到你的进程上。
注意:
- 你可能需要重新启动服务器才能使其生效。
- IDE 将要求你安装
gops
,请确保安装它。如果没有安装gops
,你将看不到任何正在运行的进程。
英文:
I followed the following procedure to start my golang server in debug mode. (You might want to use some other method than creating a makefile for starting your server maybe)
- Create a makefile
- Define your server/worker startup command in the file
- Run it by creating a new configuration for
Makefile
- Go to
Run -> Attach to process -> choose your running server
This will attach the debugger to your process.
Note:
- You might need to restart your server for this to work.
- The IDE will ask you to install
gops
, do install it. Without installinggops
you won't see any running process.
答案7
得分: -1
我右键点击项目(内部的main.go文件),选择“调试...”,然后它就可以工作了。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论