Go 的调试

调试之前,项目文件夹中需要运行命令:

go mod init my-app-name

go debugger tool dlv,在配置 vscode go 开发环境时就可以安装:
在这里插入图片描述
然后 go code 中可以设 breakpoint,选择 vscode -> run -> start debugging,就可以开始调试代码,例如 step over, continue, 等等,这和其他语言比方说 C++ 的调试方法几乎完全一样。
在这里插入图片描述

如果要调试会从键盘读取输入的app,在工程中新增一个名称为.vscode 的文件夹,包含两个文件:

  1. tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "delve",
            "type": "shell",
            "command": "dlv debug --headless --listen=:23456 --api-version=2 \"${workspaceFolder}\"",
            "isBackground": true,
            "presentation": {
                "focus": true,
                "panel": "dedicated",
                "clear": false
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": {
                "pattern": {
                    "regexp": ""
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": {
                        "regexp": ".*"
                    },
                    "endsPattern": {
                        "regexp": ".*server listening.*"
                    }
                }
            }
        }
    ]
}
  1. 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": "Connect to server",
            "type": "go",
            "request": "attach",
            "preLaunchTask": "delve",
            "mode": "remote",
            "remotePath": "${workspaceFolder}",
            "port": 23456,
            "host": "127.0.0.1",
            "cwd": "${workspaceFolder}"
        }
    ]
}

然后重启 vscode 开始调试,输出不再出现在 debug console, 而是出现在 terminal window。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值