Home  >  Article  >  Backend Development  >  How to Set Up and Use the Delve Debugger in Visual Studio Code for Go Development?

How to Set Up and Use the Delve Debugger in Visual Studio Code for Go Development?

Linda Hamilton
Linda HamiltonOriginal
2024-11-10 12:10:03497browse

How to Set Up and Use the Delve Debugger in Visual Studio Code for Go Development?

Using the Delve Debugger in Visual Studio Code for Go Development

If you're experiencing issues using the Delve debugger within Visual Studio Code for Go development, the following detailed steps will guide you through the setup process:

  1. Install and Configure Go Environment:

    • Ensure the latest version of Go is installed and the GOROOT and GOPATH environment variables are set correctly.
    • Add $GOPATH/bin to the OS PATH environment variable.
    • Set the environment variable GO15VENDOREXPERIMENT = 1.
  2. Install Visual Studio Code:

    • Download and install Visual Studio Code.
  3. Install Go Extension in VS Code:

    • Launch VS Code Quick Open (Ctrl P) and enter "ext install Go" to install the Go extension.
  4. Open Workspace and Source Code:

    • Open the workspace folder containing your Go code (Ctrl Shift E).
  5. Open Debugger and Set Breakpoint:

    • Open the Debugger panel (Ctrl Shift D).
    • Set a breakpoint on the line of code you want to debug (e.g., F9).
  6. Start Debugging:

    • Press F5 to start debugging and select "Go" if prompted for an environment.
  7. Debug Controls:

    • Step Over: F10
    • Step Into: F11
    • Step Out: Shift F11
    • Stop Debugging: Shift F5
    • Restart Debugging: Ctrl Shift F5

Launch.json Configuration:

An unmodified launch.json file for reference:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}",
            "env": {},
            "args": [],
            "showLog": true
        }
    ]
}

Sample Go Code to Demonstrate Debugging:

package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
    i := 101
    fmt.Println(i)
}

Expected Output:

The debugger will break at the breakpoint set and allow you to step through your code, inspect variables, and control the program's execution.

The above is the detailed content of How to Set Up and Use the Delve Debugger in Visual Studio Code for Go Development?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn