Debugging in Go

arjun dhar
3 min readMay 8, 2020

--

Notes from novice to expert

In general if GOPATH is set correctly and project folder is as per conventions then no additional config is needed. Simply be on a File thats a main or a Unit Test and click the Deug Icon, and debug away

Sometimes the port seems blocked; simply restart VSCode or ensure you are on a file thats runnable/testable.

If there are any deviations from the standard path convetnions; then can tweak the config files. Refer to VSCode Config-3 Debug config.

Also read Debugging in VS Code as a basic primer

Debug a Server

@See Also: Debugging-Go-code-using-VS-Code @See Also: Update documentation about remote debugging Launch application using:

dlv debug --listen=:2345 --headless=true --api-version=2 --log

@ .vscode\launch.json Add the launch config in the array as example:

{
"name": "Remote",
"type": "go",
"request": "launch",
"mode": "remote", // or attach
//"remotePath": "C:\\Users\\EricS\\go\\src\\test-struct",
"port": 2345,
"host": "127.0.0.1",
//"program": "C:\\Users\\EricS\\go\\src\\test-struct",
"env": {}
}

Note: mode remote is deprecated and replaced by attach

OR

We can attach or Launch using debug mode within VSCode.

{
"name": "Remote",
"type": "go",
"request": "launch",
"mode": "debug", // or attach if a dlv process is already running
"remotePath": "", // <-- blank
"port": 2345,
"host": "localhost",
"program": "${workspaceRoot}\\eaciit\\ride\\app\\main\\data-visual\\web-services",
"env": {},
// The following are optional.
// See https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code.
"showLog": true,
"trace": "verbose",
//"logOutput": "rpc"
}

Error on create breakpoint file does not exist (unverified breakpoint)

On setting a break point on a running server we may get the above error. Cross check the path in the error. If the path is valid but it says it cannot find the file; then the issue is that of using a Symlink or some GOPATH issue. Fix: Set the GOPATH so the file is found as part of the path, alternatively as a Quick-Fix, one can in configs.code-workspace > settings > go.inferGopath:

{
"folders": [
...
],
"settings": {
...
"go.inferGopath": true,
}
}

Configs

VSCode Config-1

/*{
// https://code.visualstudio.com/docs/languages/go
"[go]": {
"editor.insertSpaces": false,
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
},
"go.gopath": "D:\\MyWork\\my_project\\code\\some-middleware",
"go.inferGopath": true,
// Setup default command prompt terminal as DOS
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
"go.gocodeAutoBuild": true,
}*/
{
"window.zoomLevel": 0,
"editor.fontSize": 13,
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
"go.formatTool": "gofmt", //"goreturns",
"go.useLanguageServer": true,
"go.autocompleteUnimportedPackages": true,
"go.gocodeAutoBuild": true,
// Added
"go.docsTool" :"gogetdoc",
"go.delveConfig": {
"dlvLoadConfig": {
"followPointers": true,
"maxVariableRecurse": 1,
"maxStringLen": 64,
"maxArrayValues": 64,
"maxStructFields": -1
},
"apiVersion": 2,
"showGlobalVariables": true
},
//Added later
"[go]": {
"editor.insertSpaces": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": false
}
}
}

.code-workspace or workspace.json:

{
"folders": [
{
"path": "D:\\MyWork\\projectX\\src"
}
],
"settings": {
"go.autocompleteUnimportedPackages": true,
"go.formatTool": "gofmt", //"goreturns",
"go.useLanguageServer": true,
"go.gocodeAutoBuild": true,
}
}

Note: You can see all Go Settings here and here

VSCode Config-2 Debug <project>\.vscode\launch.json: (Which appears when there is an error and a dialog button asks to open it)

{
// 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": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {
},
"args": []
}
]
}

<project>\.vscode\launch.json: For debugging config @See Also: Debugging-Go-code-using-VS-Code, VS code Debugging VSCode Config-3 Debug

{
// 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
// https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code
// https://code.visualstudio.com/docs/editor/debugging
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}\\src",
"cwd": "..\\",
"env": {
"GOPATH":"C:\\installed\\gopath;D:\\MyWork\\my_project\\code\\some-middleware"
},
"args": ["--printer-ip=localhost", "--data-path=.\\test\\resources\\data.csv"]
}
/*,{
"name": "Attach to Process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": 0
}*/
]
}

WARNING: If the workspace folder is different than say the main program, "pogram":"${workspaceFolder}\src\main.go"; then it finds the file but relative paths in the application go wonky as they assume base folder as where the main program is running from not the workspace folder. For this reason, any relative paths should assume where the program runs as base path.
The way to get around is use cwd that can use a relative path to program folder.

Cannot move, delete or Access Denied errors : Check if any other process is running or has locked the file system or shared resource.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

arjun dhar
arjun dhar

Written by arjun dhar

Software development enthusiast since I was 8 yrs old. Love communicating on anything regarding innovation, community development … ∞

No responses yet

Write a response