登录
首页 >  Golang >  Go问答

解决 golang 中使用 vscode 进行调试测试的问题

来源:stackoverflow

时间:2024-02-08 16:00:23 336浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《解决 golang 中使用 vscode 进行调试测试的问题》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我正在 vscode 上使用一些全局变量运行 golang 测试,这些全局变量在到达测试函数之前进行初始化。

我的程序中的全局变量从 launch.json 文件 (os.args) 中指定的运行行参数获取其值。

出于某种原因,当我运行测试时,我的参数与 launch.json 文件中指定的参数不同。

这些是我的启动文件的内容:

{
    // use intellisense to learn about possible attributes.
    // hover to view descriptions of existing attributes.
    // for more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "launch file",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${file}",
            "args": [".", "from", "to", "csv", "hl7"]
        }
    ]
}

这些是我不断获得的不同值:

os.Args[0] = //path to the __debug_bin.exe
os.Args[1] = "-test.run"
os.Args[2] = "^TestInit$"

在我的 launch.json 文件中,我总共有 6 个参数,这导致我在尝试运行测试时出现越界错误。

所以我的问题是如何更改它以便使用我在启动文件中指定的参数运行测试?


正确答案


codelens暂时不支持此功能,请查看此处codelens: a way to use selected launch.json configuration in run test | debug

但是你可以这样做

  1. 在用户设置中设置 "go.testflags": ["-v", "-args", "from", "to", "csv", "hl7"] (文件路径) "code\用户\settings.json"

你可以这样检查

t.Log(os.Args[3])
t.Log(os.Args[4])

...

今天关于《解决 golang 中使用 vscode 进行调试测试的问题》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>