Debugging Go with VSCode: From Bug to Breakpoint
Debugging code is an art. It requires patience, logic, and the right tools. In a developer's daily routine, identifying and fixing bugs is essential—whether it's handling a critical production issue or tweaking an unexpected behavior.
But have you ever wondered where the term "debugging" comes from?
The Origin of Debugging
The term debugging literally came from the need to remove a bug (an insect) from a system. In 1947, computer scientist Grace Hopper was working on the Harvard Mark II computer when her team encountered an unexpected failure.
Upon investigation, they discovered a moth trapped in a relay, causing a malfunction. They logged the incident in the system’s records with the note:
"First actual case of bug being found."
Since then, the term debugging has been used to describe the process of finding and fixing errors in software.
Now that we understand the history, let’s get to what really matters: debugging Go in VSCode.
Setting Up a Simple Go Project for Debugging
Before diving into debugging, let's set up a basic Go project structure in VSCode.
For a simple Go project structure that includes this file and allows for debugging in VSCode, follow this structure:
Project Structure
Step 1: Creating the Project
Now, create a main.go file and add any code you want. On my side, I just did it to ease the understanding.
Setting Up Debugging in VSCode
To enable breakpoints and variable inspection, we need to configure a launch.json file inside the .vscode/ folder.
Step 2: Creating the Debugging Configuration
Create a .vscode/launch.json file and paste the following configuration:
Understanding the Debug Configuration in VSCode
To properly debug your Go application in VSCode, you need to configure the launch.json file inside the .vscode/ folder. Below is a breakdown of each property in the configuration and what it does.
🔹 name – The name of the debug configuration. This appears in the VSCode Run and Debug dropdown.
🔹 type – Specifies the type of debugger being used. Since we are debugging a Go application, this should be set to "go".
🔹 request – Defines how the debugger starts. The most common value is "launch", which starts the program from the beginning. Another possible value is "attach", used to connect to an already running process.
🔹 mode – Determines how the program will be executed in debug mode. The "debug" mode compiles the application and starts it with full debugging support.
🔹 program – Specifies the path to the Go file that should be executed when debugging. In our case, it points to "${workspaceFolder}/main.go", meaning it will run main.go inside the current workspace.
🔹 env – Defines environment variables that will be available during debugging. This is useful for setting up API keys, database connections, or different application environments.
This allows us to simulate different configurations, such as changing API keys or database connections dynamically.
Debugging in VSCode: Step-by-Step Guide
Step 3: Running the Debugger
Now, let’s run the debugger and inspect variables step by step.
Open VSCode and go to the Run and Debug panel (Ctrl + Shift + D or Cmd + Shift + D on macOS).
Select "Launch Go Debug" from the dropdown.
Click the Run (F5) button.
Using Breakpoints and Inspecting Variables
Step 4: Setting Breakpoints
Open main.go in VSCode.
Click to the left of the line number where you want execution to pause.
A red dot will appear, indicating a breakpoint.
Step 5: Debugging Controls
Once the debugger pauses execution, you can start exploring your code step by step using breakpoints and debugging controls. These controls help you understand the execution flow, inspect variables, and detect issues in your program.
🔹 Debugging Navigation: Step Over vs. Step Into
When execution is paused at a breakpoint, you have two main ways to navigate through your code:
Step Over (F10) – Moves to the next line of code, but does not enter functions.
Step Into (F11) – Jumps inside functions, allowing you to debug their internals.
Final Thoughts
Using environment variables, breakpoints, and variable inspection, you can efficiently debug Go applications in VSCode.
This debugging workflow helps you:
Identify logic errors.
Understand how variables change at runtime.
Test different values dynamically without modifying the source code.
Start experimenting and let me know if you have any questions!
Android Developer | Mobile Software Engineer | Kotlin | Jetpack Compose | XML
5moGreat content!! Thanks for sharing!!
Senior React Developer | Full Stack Developer | JavaScript | TypeScript | Node.js
5moFundamentals are the backbone of great content! Simple topics often spark the most valuable discussions. Keep sharing your insights!
Software Engineer | Java | Kotlin | Spring | AWS | JUnit | Docker
5moGreat breakdown! Debugging is such an underrated skill, and having the right setup in VSCode makes a huge difference.
Software Engineer | React | Node
5moNice tips! Thanks for sharing!
Gen AI | LLMs | RAG | AI Solutions Architecture | MLOps & AIOps | Kotlin | Go | Flutter | .NET 8 | Java | Hexagonal Arch | gRPC | Docker | K8s | Terraform | Vertex AI | AWS | GCP | Azure | Hands-on AI in Production
5moThanks for sharing.🚀