how to debug golang in vscode

1.6k views Asked by At

platform: windows 10 golang version : go1.21.1 windows/amd64 dlv version : Delve Debugger Version: 1.21.1

launch json:

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

when I start debug with vscode , It shows :

DAP server listening at: 127.0.0.1:48323 Build Error: go build -o xxxx\project\rpc\client\src__debug_bin3964848569.exe -gcflags all=-N -l . client.go:4:2: package fmt is not in std (C:\Users\Admin\go\src\fmt) client.go:5:2: package net/rpc is not in std (C:\Users\Admin\go\src\net\rpc) package rpc/client/src: cannot find package (exit status 1)

but project can be build successful. How did I solved this problem? thanks

but project can be build successful. How did I solved this problem? thanks

2

There are 2 answers

0
Tancelin MAZZOTTI On
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}",
            "args": [
                "-o", "output.log"
            ]
        }
    ]
}
0
Zargham Ahmed On

After opening your vs code click on main.go file or a file whose package name is (package main) then launch your debugger.

link for more detail.