【问题标题】:How to pass command-line arguments in debug mode in VSCode with golang如何使用 golang 在 VSCode 中以调试模式传递命令行参数
【发布时间】:2020-03-27 06:10:44
【问题描述】:

我在使用 golang 在 VSCode(调试模式)中传递命令行参数时遇到困难。

下面是小代码示例和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"
            ]
        }
    ]
}

*flag1Ptr 的输出始终为“假”,但应该为“真”。

更新:关闭 VSCode 并重新打开后问题消失(我在 Mac(osX) 上)

【问题讨论】:

  • 我测试了你的代码,它对我有用。我使用 vscode 调试器得到了 true 的值。
  • 很奇怪。我正在使用 Mac (osX)。我不知道为什么它不起作用。
  • 我在一台 linux 电脑上测试过。我没有 Mac 来测试它。
  • 我只是在启动文件中添加了“trace”:“log”,它没有输出任何新内容。我怀疑我的设置没有运行 launch.json 文件
  • 我不确定发生了什么(我确实关闭了 VSCode 并重新打开了它)但它开始工作了。 trace:log 文件也开始工作了。

标签: debugging go command-line visual-studio-code arguments


【解决方案1】:

列出所有参数,例如, 命令是:

"hello.exe -in InfoEntity.java -out aa.out"

调试配置是:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "env": {},
            "args": ["-in", "InfoEntity.java", "-out", "aa.out"]
        }
    ]
}

【讨论】:

    猜你喜欢
    • 2019-11-13
    • 1970-01-01
    • 2016-05-20
    • 2020-07-02
    • 2020-03-07
    • 2021-09-30
    • 2010-09-11
    • 2016-02-22
    • 2019-07-16
    相关资源
    最近更新 更多