Coding, How to

Pass arguments to Golang program when debugging with VSCode

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

"version": "0.2.0",
"configurations": [
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "",
"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

view raw
hosted with ❤ by GitHub

And we’re away…

Screen Shot 2017-11-01 at 11.52.36


N.B The behavior is a bit odd, as the code here suggests that the “–” should be appended for you. However, this did not work for me, may be fixed in the future so double check!