Skip to content

Latest commit

 

History

History
142 lines (87 loc) · 3.91 KB

go-delve.md

File metadata and controls

142 lines (87 loc) · 3.91 KB

Go - Delve

To debug Go files, you can use the Delve DAP server.

Let’s debugging the following test.go file:

package main

import "fmt"

func main() {
    var user = "world";
    fmt.Println("hello", user);
}

Set Breakpoint

Configure DAP server

  1. Install Delve.

  2. After the installation, you should have the dlv command available (make sure to close and reopen your IDE to ensure the dlv command is properly recognized).

If you open a terminal and run the following command:

dlv dap

You should see in the terminal traces like this:

DAP server listening at: 127.0.0.1:60732
  1. Create a DAP Run/Debug configuration:

    DAP Configuration Type

  2. In the Server tab, click on create a new server:

    Create a new server

  3. It opens a new dialog to create DAP server, select Go - Delve template: Select Template

  4. After clicking on OK button, it will select the new server and pre-fill configurations:

Select New server

This will automatically populate:

  • the server name
  • the command which starts the DAP server which should look like this:
dlv dap
  • the Connect to the server by waiting option is set to Log pattern before processing with:
DAP server listening at: ${address}:${port}

This means the DAP (Debug Adapter Protocol) client will connect to the DAP server when this trace appears in the console:

dlv dap
DAP server listening at: 127.0.0.1:60732

Here ${port} will be extracted and client will connect to the 60732 port.

  1. Enable DAP server traces

If you wish to show DAP request/response traces when you will debug:

Show DAP traces

you need to select Trace with verbose.

Set verbose traces

Configure file mappings

To allows settings breakpoints to Go files, you need configure mappings in the Mappings tab. As you have selected Go - Delve server, it will automatically populate the file mappings like this:

File mappings

Configure the Go file to run/debug

  1. Fill in the Configuration tab:
  • the working directory (usually the project's root directory)
  • the path to the test.go file.

DAP Configuration/Configuration

  1. Select Launch as Debug mode.
  2. The DAP parameters of the launch should look like this:
{
   "type": "go",
   "name": "Launch Go file",
   "request": "launch",
   "program": "${file}",
   "cwd": "${workspaceFolder}"
}

When the run configuration starts:

  • ${workspaceFolder} will be replaced with the working directory you specified.
  • ${file} will be replaced with the full path to test.go.

Set Breakpoint

After applying the run configuration, you should set a breakpoint to files which matches file mappings. Set a breakpoint in the test.go file:

Set Breakpoint

Debugging

You can start the run configuration in either Run or Debug mode. Once started, you should see DAP traces in the console:

Debugging / Console

You will also see Threads and Variables:

Debugging / Threads

Language Support

If you need language support for Go (completion, validation, etc) you can configure the Go Language Server

Go demo