I am having difficulty in passing command-line arguments in VSCode (debug mode) with golang.
Below is the small code example and the launch.json:
package main
import (
"flag"
"fmt"
)
func main() {
flag1Ptr := flag.Bool("flag1", false, "flag1 is a flag")
flag.Parse()
fmt.Println(*flag1Ptr)
fmt.Println("Hello, world")
}
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": [
"-flag1"
]
}
]
}
The output is always "false" for *flag1Ptr but it should be "true".
Update: The issue disappeared after closing down VSCode and reopening it (I am on a Mac(osX))
To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.
To access all command-line arguments in their raw format, we need to use Args variables imported from the os package . This type of variable is a string ( [] ) slice. Args is an argument that starts with the name of the program in the command-line. The first value in the Args slice is the name of our program, while os.
Open a file to debug (either package main source file or the test file) in the editor, and select the Run and Debug button from the Run view. Alternatively, you can start debugging using Start Debugging (F5) command from the Run menu or from the Command Palette (Linux/Windows: Ctrl+Shift+P, Mac: ⇧+⌘+P).
To debug a program, execute the dlv debug command. Attach the filename at the end of this command, and debugging will start. For example, if you want to debug the main.go file, run the command dlv debug main.go . It is also known as “delve server” because this is a running process waiting for instructions.
list all params, for example, command is:
"hello.exe -in InfoEntity.java -out aa.out"
the debug config is:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": ["-in", "InfoEntity.java", "-out", "aa.out"]
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With