how to debug golang in vscode – Go

by
Maya Patel
argocd debugging dlv launch visual-studio-code

Quick Fix: Utilize Visual Studio Code’s built-in debugging capabilities to troubleshoot your Go code. From the ‘Run’ menu, select ‘Start Debugging’ or use the keyboard shortcut (F5). This will launch the debugging session.

The Problem:

A user is trying to debug a Go program in Visual Studio Code, but they are encountering build errors. Specifically, the error message is "package fmt is not in std (C:\Users\Admin\go\src\fmt)" and "package net/rpc is not in std (C:\Users\Admin\go\src\net\rpc)". The problem occurs when starting the debug session, and the build is successful when done manually. The user is seeking a solution to resolve these build errors and debug the program successfully.

The Solutions:

Solution 1: Change debug mode to “auto”

To fix the issue where debugging in Visual Studio Code with Delve resulted in the error “package fmt is not in std”, the following solution was implemented:

  1. Open the launch.json file located in the .vscode folder of your project directory.
  2. Find the “configurations” section.
  3. Identify the “mode” property within the “configurations” section.
  4. Change the value of the “mode” property from “debug” to “auto”.
  5. Save the launch.json file.

With this modification, the debug mode for Go programs is set to "auto." This change allows Delve to automatically detect the best debugging mode based on the project’s configuration.

Solution 2: Debugging Main Package

To debug a Go program in VSCode, ensure that you start the debugging session from a file belonging to the main package (package main).

Follow these steps:

  1. Open the main.go file or any file belonging to the main package in VSCode.
  2. Click on the Debug icon in the VSCode toolbar or press F5.
  3. VSCode will start the debugging session and launch the Delve debugger.

This approach ensures that the debugger correctly initializes and attaches to the correct process.

Q&A

How to fix build error in vscode when debugging golang?

Change the debug mode to auto.

How to launch debugger in vscode after opening vs code?

Open a file whose package name is (package main), then launch your debugger.

Video Explanation:

The following video, titled "How to debug Go Web App using VS Code? - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

How to debug Go Web App using VS Code? 6.9K views · 2 years ago #golang #vscode #debug ...more. CoderVlogger. 7.49K. Subscribe.