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);
}
-
After the installation, you should have the
dlv
command available (make sure to close and reopen your IDE to ensure thedlv
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
-
Create a DAP Run/Debug configuration:
-
In the
Server
tab, click oncreate a new server
: -
It opens a new dialog to create DAP server, select
Go - Delve
template: -
After clicking on
OK
button, it will select the new server and pre-fill configurations:
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 toLog 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.
- Enable DAP server traces
If you wish to show DAP request/response traces when you will debug:
you need to select Trace
with verbose
.
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:
- Fill in the
Configuration
tab:
- the
working directory
(usually the project's root directory) - the path to the
test.go
file.
- Select
Launch
asDebug mode
. - 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 totest.go
.
After applying the run configuration, you should set a breakpoint to files which matches file mappings.
Set a breakpoint in the test.go
file:
You can start the run configuration in either Run or Debug mode. Once started, you should see DAP traces in the console:
You will also see Threads
and Variables
:
If you need language support for Go (completion, validation, etc) you can configure the Go Language Server