I’m doing some work on a golang project, the code takes a relative file path in as an argument.
Now I have Delve, VSCode and VSCode-Go installed which means I can have a nice interactive debugging session. The only snag I hit was that it wasn’t clear how to pass in my arguments when the debugger started my go code.
The trick is the use a double dash to indicate which arguments should go to the code, other args, before this, will go to the delve debugger. Also don’t forget to use “cwd” to set the working dir used when debugging.
Here is an example with comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.2.0", | |
"configurations": [ | |
{ | |
"name": "Launch", | |
"type": "go", | |
"request": "launch", | |
"mode": "debug", | |
"remotePath": "", | |
"port": 2345, | |
"host": "127.0.0.1", | |
"program": "${workspaceRoot}/cmd/traefik", | |
"env": {}, | |
// The — here is key, telling delve to pass arguments after it to your code | |
"args": ["—", "-c", "config.lgpriv.toml"], | |
// This controls the Working directory the code runs in, as my config.lgpriv.toml in my root | |
// I want the working dir to be the workspace root | |
"cwd": "${workspaceRoot}", | |
"showLog": true | |
} | |
] | |
} |
And we’re away…
N.B The behavior is a bit odd, as the code here suggests that the “–” should be appended for you. https://github.com/Microsoft/vscode-go/blob/master/src/debugAdapter/goDebug.ts#L306. However, this did not work for me, may be fixed in the future so double check!