简体   繁体   中英

How to use Delve debugger in Visual Studio Code

I have installed the Go extension for VS Code, but unable to make it work.

"dlv debug" works alright from the terminal.

dlv debug src/github.com/user/hello

The launch.json :

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceRoot}",
            "env": {},
            "args": []
        }
    ]
}

Do you know how to set it up?

For using Delve debugger in Visual Studio Code with Golang, do the following steps:

( Note: for Windows OS replace all $GOPATH with %GOPATH% )
  • Install Latest Golang and set GOROOT and GOPATH
  • Add $GOPATH/bin to your OS PATH environment variable.
  • set environment variable: GO15VENDOREXPERIMENT = 1
  • run: go get github.com/derekparker/delve/cmd/dlv and make sure dlv binary generated in your $GOPATH/bin
  • Install Visual Studio Code
  • Launch VS Code Quick Open ( Ctrl + P ), paste this command: ext install Go , and press enter.
  • click install Rich Go language support for Visual Studio Code
  • click Enable and restart Visual Studio Code
  • Inside Visual Studio Code Open Folder Ctrl + Shift + E , eg: $GOPATH\src\hello\
  • Then Open hello.go from that folder (or make new file Ctrl + N and save it on this folder):
package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
    i := 101
    fmt.Println(i)
}
  • Then Open Debugger Ctrl + Shift + D
  • on this line: i := 101 press F9 to set or toggle beakpoint.
  • Press F5 to start debugging or to Run the application, if asked to select environment: select Go .
  • Press F10 to Step Over.
  • Press F11 to Step Into.
  • Press Shift + F11 to Step Out.
  • Press Shift + F5 to Stop Debugging.
  • Press Ctrl + Shift + F5 to Restart Debugging.

My launch.json untouched:

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

Result:

在此处输入图像描述

This launch.json worked for me to run the Golang debugger in VSCode:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch file",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${file}",
      "env": {
        "PATH": "/usr/local/go/bin:${fileDirname}"
      },
      "args": []
    }
  ]
}

VSCode Variables Reference : If file /home/your-username/your-project/folder/main.go is open in VSCode and

directory /home/your-username/your-project is your root workspace, then

${file} = /home/your-username/your-project/folder/main.go

${fileDirname} = /home/your-username/your-project/folder


My specific values:

$GOROOT: /usr/local/go

$GOPATH: /Users/myname/code

${file}: /Users/myname/code/src/github.com/githubName/appName/main.go

${fileDirname}: /Users/myname/code/src/github.com/githubName/appName

You have to do three things here:

  • Install Delve. Looking at your question it seems that you have already installed Delve.
  • Install Go extension for VS Code. The extension can be found at : https://github.com/golang/vscode-go
  • Install dlv tool for Go. You can do that by opening up Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and select Go: Install/Update Tools then search/select dlv

Now you can start debugging with delve in VS code.

More more detailed instruction please follow : https://dev.to/nyxtom/debugging-in-go-in-vs-code-1c7f

FTA (in case it is hard to find), if when using delve and you get cannot find package error even though your GOPATH is set correctly, check out this bug of vscode-go , it is affecting both MAC OS and Linux, as of October, 2017.

The solution is posted there as well:

... adding the GOPATH as an env var in the env property in the launch.json file solved the problem

Content launch.json for gdb and delve

{
// Используйте IntelliSense, чтобы узнать о возможных атрибутах.
// Наведите указатель мыши, чтобы просмотреть описания существующих атрибутов.
// Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "Delve",
        "type": "go",
        "request": "launch",
        "mode": "debug",
        "remotePath": "",
        "port": 2345,
        "host": "127.0.0.1",
        "program": "${workspaceRoot}/src/hello/hello.go",
        "env": {},
        "args": [],
        "showLog": true
    }
   ,
    {
        "type": "gdb",
        "request": "launch",
        "name": "GDB",

        "target": "${workspaceRoot}/src/hello/hello",
        "cwd": "${workspaceRoot}",
        "linux": {
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
    }
]

}

If you got error : Failed to continue: "Cannot find Delve debugger. Install from https://github.com/go-delve/delve & ensure it is in your Go tools path, "GOPATH/bin" or "PATH"."

Maybe you can use my solution (on mac) :

  1. First follow how to install delve on here : https://github.com/go-delve/delve/tree/master/Documentation/installation
  2. Open your vscode, go to your project directory, then on vscode terminal : go mod init example.com/m/v1
  3. run : go get github.com/go-delve/delve/cmd/dlv
  4. Open mac terminal and run : open ~/.zshrc (if not exist, run : touch ~/.zshrc)
  5. Make sure that file contains this script :

export GOPATH=$HOME/go

export PATH=$PATH:$GOPATH/bin

export PATH=$PATH:$GOROOT/bin

export GOROOT=/usr/local/go

  1. Then copy all script on .zshrc
  2. Still on mac terminal, run : open ~/.bashrc (if not exist, run : touch ~/.bashrc)
  3. Paste the script to .bashrc file
  4. Then save .zshrc and .bashrc
  5. close terminal & close vscode
  6. Open vscode again
  7. The last thing, make sure you already create launch.json (in .vscode folder), then you can follow my launch.json :
{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}",
            "args": []
        }
    ]
} 
  1. Then finish ! You can try again run your program and follow step2 using delve on accepted answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM