Before debugging, the command needs to be run in the project folder:
go mod init my-app-name
The go debugger tool dlv
can be installed when configuring the vscode go development environment:
then you can set a breakpoint in go code, select vscode -> run -> start debugging, you can start debugging the code, such as step over, continue, etc. Other languages such as C++ have almost exactly the same approach to debugging.
If you want to debug an app that reads input from the keyboard , add a new folder named .vscode to the project, containing two files:
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "delve",
"type": "shell",
"command": "dlv debug --headless --listen=:23456 --api-version=2 \"${workspaceFolder}\"",
"isBackground": true,
"presentation": {
"focus": true,
"panel": "dedicated",
"clear": false
},
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"pattern": {
"regexp": ""
},
"background": {
"activeOnStart": true,
"beginsPattern": {
"regexp": ".*"
},
"endsPattern": {
"regexp": ".*server listening.*"
}
}
}
}
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Connect to server",
"type": "go",
"request": "attach",
"preLaunchTask": "delve",
"mode": "remote",
"remotePath": "${workspaceFolder}",
"port": 23456,
"host": "127.0.0.1",
"cwd": "${workspaceFolder}"
}
]
}
Then restart vscode to start debugging, the output no longer appears in the debug console, but in the terminal window.