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

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

Mary-Kate Olsen
Release: 2024-11-10 00:11:03
Original
1030 people have browsed it

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

Setting Up the Delve Debugger in Visual Studio Code for Go Development

Facing difficulties getting the Go extension for VS Code to work, specifically for Delve debugging? Here's a comprehensive guide to resolve this issue.

Prerequisites:

  • Install the latest Go version and set environment variables GOROOT and GOPATH.
  • Add $GOPATH/bin to your OS PATH environment.
  • Set the environment variable GO15VENDOREXPERIMENT to 1.
  • Via the command go get github.com/derekparker/delve/cmd/dlv, ensure the dlv binary is present in $GOPATH/bin.
  • Install Visual Studio Code.

VS Code Setup:

  1. Launch VS Code Quick Open (Ctrl P), enter "ext install Go", and press Enter.
  2. Install "Rich Go language support for Visual Studio Code".
  3. Enable and restart VS Code.
  4. Open the working directory (e.g., $GOPATHsrchello).
  5. Create a new file (Ctrl N) or open hello.go from that folder:
package main

import "fmt"

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

Debugging in VS Code:

  1. Open the debugger (Ctrl Shift D).
  2. Set a breakpoint on the line: i := 101.
  3. Start debugging (F5).
  4. Step through the code:

    • F10: Step Over
    • F11: Step Into
    • Shift F11: Step Out
  5. Stop debugging (Shift F5) or restart debugging (Ctrl Shift F5).

Sample launch.json:

{
    "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
        }
    ]
}
Copy after login

Upon following these steps, Delve debugging should work seamlessly in your VS Code for Go development.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template