You are already familiar with the debugging process from the command line with Delve. This is a simple and effective way. However, it takes a lot of time and may not always be clear enough.
So, if you are using the GoLand IDE, there is one more tool at your disposal. It allows you to make the debugging process easy and intuitive. This is the IDE's built-in debugger.
About the debugger
This may come as a surprise, but the GoLand debugger uses Delve as its debugging tool to work. Therefore, in it, you can see the entire set of functionality, just like in console debugging. Only in this case, the IDE gives you the opportunity to do all this in a graphical shell in the usual ways.
Debugging looks almost like a normal program launch, only with some special fields.
The debugger is already set up to give you all the data you need. However, if you want to customize something for yourself, you can always do it in the GoLand Settings>Build, Execution, Deployment>Debugger settings window. We will not cover them in detail, but you can read them in the documentation.
So how do you use this tool? Let's take a closer look at all the steps.
Please note that you can change the code while the debugger is running, but to apply these changes, you need to restart the debugger again.
Breakpoints
The first thing you should do is to set breakpoints. Breakpoints are special markers that indicate places where the debugger needs to freeze the state of the program. Without them, your code will run as if it were running normally, and you will only see the result of its execution. To set breakpoints, click in the field to the left of the code. To remove an unnecessary breakpoint, click on it again.
Launch and main elements
You can use one of the following methods to run the program in debug mode. All of these methods launch the debugger in the same way, so which one to use is up to you! You can use the main menu Run>Debug or both launch menu 'Debug go build filename.go' on the top right and to the left of the main function.
You will open an additional tab in the bottom panel – Debug (4). On this panel, you can find the following debug controls:
Debugger/Console switch.
The current frame stack.
A field with the current values of the variables, arguments, and context of the selected frame.
Debug process control menu.
Step-by-step debugging.
On the Debugger tab, you can monitor values while debugging. The Console displays the progress of the program and let you enter data while the program is running or monitor console output.
Frames
Frame corresponds to the active function or method. They store the values of variables, arguments, and context.
As you can see, this window lists all the functions that are currently running in order:
main.isEven(line:17) – the program is currently stopped at this line
main.main(line:8) – isEven() function call line
runtime.main, runtime.goexit – system functions that control program operation are added at compile time
Variable values
Why are you running the debugger? Of course, to see which way the program execution will go and what values the variables will take at a particular point in the execution. This field can give you information with all the values in the current execution location and even more!
When you need to see the value of a specific element of an array, map, or list you can add it to the trace using normal Go syntax. Like entry array[5] or someMap["key"] for an array or map. And, if the element is available in the current scope of the program, its value will be shown. All new tracing dimensions will be marked by a special glasses sign. These values will be with you until you remove them from tracking.
Similarly, you can simply calculate the value of some expression if you type it into a line and press enter. But remember, when you leave this step the value will be gone.
Controlling the debugging process
The Debug process control menu (4) is a top-level menu that allows you to control the debugging process in large steps. Most of the controls in it are intuitive and will not cause you problems. But you can take a quick look at the elements whose behavior can cause difficulties.
For example Resume Program can resume program execution after a pause and go to the next breakpoint. A question may arise "Why do we need a pause if we have breakpoints?". It's simple – sometimes your program can get stuck while executing a piece of code, like a cycle, and not reach the breakpoint. In this case, it may be necessary to pause it and see the reasons.
Also, you may want to View breakpoints... It opens a window with all breakpoints and their settings. And sometimes you may want to run the program further without breakpoints but not remove them at all. In this case, you can use Mute breakpoints.
When you set breakpoints, you may want to set breakpoints on every line of your program to control execution at any point. But this is not necessary. You can set breakpoints at key points and then step through the program. You can try it with Step by step debugging (5).
In this menu, the most interesting and useful and at the same time incomprehensible can be Step over, Step into and Step out. All of these elements are used to step in relation to the function or methods. How exactly are they used? Let's find out!
Step over goes to the next line of code, even if the current one contains a call to other functions, and skips passing through them. You can use this if you don't need to watch the function called on that line executed. For example, are you sure about its calculations or is it a function of the standard library "fmt".
Using the next two you can step into the function in the current line or out the current function to one level upper. It may be useful when you need to jump inside a function and check the execution steps or you have already looked at all the necessary values in the current frame and you need to exit it quickly.
Note that if you are using the Run to cursor and there is a breakpoint set along the step path, the debugger jumps to it
Conclusion
In this topic, you got acquainted with the main features of the GoLand debugger. Below are the main points:
how to start debugging process
how to work with breakpoints
how to navigate through program execution in debug mode
how to see the values of variables, arguments
how to calculate additional values during program execution
You can always refer to the IDE documentation to learn about settings and some rarely used features of the GoLand debugger.
Now let's practice!