43

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

7
  • I tested your code and it works for me. I get the value true with vscode debugger. Commented Dec 2, 2019 at 15:42
  • Very strange. I am using a Mac (osX). I don't know why it is not working. Commented Dec 2, 2019 at 15:52
  • I’ve tested it on a linux computer. I don’t have a mac around to test it. Commented Dec 2, 2019 at 15:57
  • 1
    I am not sure what happened (I did close down VSCode and reopened it) but it started working. The trace:log file also started working. Commented Dec 2, 2019 at 16:45
  • 1
    I experienced similar things too. It looks like restarting vscode when we see something unexpected is the first thing to do. Good to see you solved your problem. Commented Dec 2, 2019 at 17:48

2 Answers 2

50

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"]
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

What if you want to pass arguments to Go itself? Like build tags etc. e.g. go run -tags=sth .
0

For me just adding launch.json to .vscode worked but not with Run Current File or Node.js options in RUN AND DEBUG menu, I needed to use Launch Program and it worked

View -> Run -> RUN AND DEBUG -> Launch Program

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.