Go debugging in VSCode

Viewed 16

is it possible to do breakpoint debugging of Go in vscode?

2 Answers

The answer to the question "Can we do breakpoint debugging of Go in VSCode?" is a resounding "Yes!"

Go debugging is supported in VSCode through the "go" extension, which is automatically installed when you install the Go extension pack. To debug your Go program in VSCode, you'll need to follow these steps:

Step 1: Install the Go extension pack

If you haven't already, install the Go extension pack from the Extensions Marketplace within VSCode. Here's how:

  1. Open VSCode.
  2. Click the Extensions icon in the left sidebar or press Ctrl + Shift + X (Windows/Linux) or Cmd + Shift + X (macOS).
  3. Search for "Go" in the Extensions Marketplace.
  4. Click the Install button next to the "Go" extension.

Step 2: Install the Delve debugger (optional but recommended)

Delve is a separate debugger for Go that provides better support for debugging Go programs. While you can use the built-in debugging capabilities of VSCode, Delve provides more advanced features and is the recommended debugger for Go.

To install Delve, run the following command in your Terminal:

go install github.com/derekparker/delve/cmd/dlv

Step 3: Create a launch configuration

A launch configuration tells VSCode how to run your Go program and attach the debugger. Create a new launch configuration file by following these steps:

  1. Open the Command Palette in VSCode by pressing Ctrl + Shift + P (Windows/Linux) or Cmd + Shift + P (macOS).
  2. Type "Create Go Launch Config" in the Command Palette, and select the "Go: Create Go Launch Config" command.
  3. Fill in the details for your launch configuration, such as the name of your Go executable and the arguments to pass to it.

Here's an example of what your launch configuration might look like:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "program": "${workspaceFolder}/main.go",
            "args": ["-args", "true"]
        }
    ]
}

Step 4: Configure the debugger

To attach the debugger to your Go program, add the following settings to your launch configuration file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "program": "${workspaceFolder}/main.go",
            "args": ["-args", "true"],
            "delenium/delve": true // Enable Delve debugger
        }
    ]
}

Step 5: Debug your Go program

Now that you've set up your launch configuration, you're ready to start debugging your Go program. Follow these steps:

  1. Open the Run View in VSCode by clicking the "Run" button in the top-right corner of the VSCode window or by pressing F5.
  2. Select the "Launch" configuration you created earlier.
  3. VSCode will compile and run your Go program. When your program reaches a breakpoint, it will pause and allow you to step through your code.

Congratulations! You've successfully set up debugging for your Go program in VSCode.

Yes, it is indeed possible to perform breakpoint debugging of Go programs in Visual Studio Code (VSCode). Visual Studio Code is a popular lightweight code editor with powerful features, including debugging capabilities for various programming languages, including Go. Here’s a step-by-step guide to enable and use breakpoint debugging for Go in VSCode:

Step 1: Install Visual Studio Code

First, make sure you have Visual Studio Code installed on your system. You can download it from the official website.

Step 2: Install Go Language

Ensure that the Go programming language is installed on your system. You can download it from the official Go website.

Step 3: Install Go extensions for VSCode

To enable Go support in VSCode, you need to install the Go extension.

  1. Open Visual Studio Code.
  2. Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X.
  3. Search for the "Go" extension.
  4. Click "Install" to install the golang.Go extension provided by the Go team.

Step 4: Configure Go Environment in VSCode

Once the Go extension is installed, you may need to configure your environment:

  1. Open a Go file or workspace in VSCode.
  2. The Go extension might prompt you to install additional tools. Follow the prompts to install them. These tools typically include delve (a debugger for the Go programming language), gopls (the Go language server), and others that enhance the Go development experience.

Step 5: Set Up and Start Debugging

Now that the Go extension and necessary tools are installed, you can proceed with setting up debugging.

  1. Open your Go project in VSCode.

  2. Set breakpoints: Open the Go source file where you want to set breakpoints. Click in the gutter to the left of the line numbers to set breakpoints.

  3. Create a launch configuration:

    • Open the Debug view by clicking on the Debug icon in the Activity Bar or pressing Ctrl+Shift+D.

    • Click on the create a launch.json file link to create a default configuration for debugging.

    • Select Go when prompted to choose an environment. VSCode will create a basic launch.json configuration file for you.

      Here is an example launch.json configuration for a simple Go program:

      {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}"
          }
        ]
      }
      

      This configuration runs the main.go file in the root of your project by default. Adjust the program path if your main.go file is located in a different directory within your workspace.

  4. Start debugging: With your breakpoints set and launch.json configured, click the green play button in the Debug view or press F5 to start debugging your Go program.

  5. Inspect variables and control execution: As the program runs, VSCode will stop at your breakpoints. You can inspect variables, evaluate expressions, and control the execution flow (step over, step into, step out) using the Debug toolbar.

Additional Tips

  • Watch Expressions: You can add watch expressions to monitor the value of variables or expressions.
  • Call Stack: The Call Stack panel shows the current state of the call stack, which can help you understand the function call sequence.
  • Debug Console: Use the Debug Console to evaluate expressions and execute arbitrary Go code at runtime.

By following these steps and utilizing the debugging features in VSCode, you can efficiently debug your Go programs and resolve issues amid your development process.