Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command-line arguments in debug mode in VSCode with golang

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))

like image 866
Dave_J Avatar asked Dec 02 '19 15:12

Dave_J


People also ask

How do you debug in VS Code with command-line arguments?

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.

How do you pass command-line arguments in Golang?

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.

How do I debug Golang code in VS Code?

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).

How do I run the go program in debug mode?

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.


1 Answers

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"]
        }
    ]
}
like image 95
nettm Avatar answered Sep 24 '22 23:09

nettm