How to debug program in Visual Studio Code

Introduction
This article will show a step by step guide on how to debug your program in Visual Studio Code. This is used especially when there is some problem with your code and you can’t figure out immediately.
Example Debugging using Visual Studio Code
Let’s say you have a program written in Go which looks like this:
This program has a function called myFunc(). Now, you have written a test which simply calls the functions myFunc():
If you want to see all the step the function goes though, you will have to enter debug mode in VS code. You start off by clicking this button which is on the left side:
Then click the debug button which will show you ‘No configurations’ at first. Click `Add Configurations` :
This will take you to ‘launch.json’ where you should select `test function` as you want to perform debug on in a function written in test:
Change the name of the function as shown below:
Now click on the list and select ‘Launch test function’:
You will need to tell the program about ‘checkpoints’ which means that it will execute the test until that point has been reached since starting from beginning all the time will not be required if the problem in your program lies somewhere in the middle or towards the end. In real life scenario you can put checkpoints based on your guesses regarding where a problem might lie in your program:
Now click debug button:
You will reach near your checkpoint now.
From then on you can move slowly through the execution of the program using ‘Step Over’, ‘Step Into’, ‘Step Out’ or ‘Continue’ buttons which also have keyboard shortcuts as you can see below. You should Step Into a function if you want to know more about it. Otherwise simply Step Over will take you to the next execution point.
In this example, we Step Into the function myFunc() to see what happens inside it. You can also see the current variable stacks in the left side of Visual Studio Code.
myFunc() eventually calls f2() function and you will end up there if you keep on Stepping Into the functions.
Concluding Remarks
This is how you can keep on going through the execution pattern of the code using the Debug feature of Visual Studio code. You can also set multiple checkpoints to quickly navigate through different sections of a long/complex program so that you quickly arrive at the problem in your code.
If you want to learn more about how to improve performance of your Go program the check out this article.
Interested in learning more Tech? Click here to take my FREE course on Kubernetes. Don’t worry if you don’t know it already, this is for beginners!
