首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Visual Studio代码中调试GO时读取输入?

如何在Visual Studio代码中调试GO时读取输入?
EN

Stack Overflow用户
提问于 2018-06-16 12:32:56
回答 4查看 5.7K关注 0票数 15

我想在Visual Studio代码1.24.0中调试一个Go程序,它是这样的:

代码语言:javascript
复制
package main

import (
   "fmt"
)

func main() {
 fmt.Println("Hello World")
 var input int
 fmt.Scanf("%d", &input)
 fmt.Printf("Hello %v", input)
}

当开始调试时,程序等待输入。我试着通过Debug控制台输入,但不起作用。像externalConsole这样的属性在launch.json for Go中似乎不起作用。有什么建议吗?

EN

回答 4

Stack Overflow用户

发布于 2021-03-18 12:35:01

我很高兴地告诉大家,找到了一个更有用的解决方案(它会在启动前自动运行任务)。

..。

launch.json:

代码语言:javascript
复制
{
    // 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}"
        }
    ]
}

tasks.json:

代码语言:javascript
复制
{
    // 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.*"
                    }
                }
            }
        }
    ]
}

它期望您使用go.mod,因为delve也会查找它来调试目录。

票数 4
EN

Stack Overflow用户

发布于 2018-06-16 13:06:22

这类似于vscode-go issue 219

解决方法(由KingRikkie提出)是:

但是有一个变通的方法。我编写了一个执行以下操作的脚本:

  • 无需优化即可编译您的应用程序,并在新窗口中内联
  • Start应用程序
  • Attach直接深入其进程id。

然后,

我在VScode中创建了一个新任务来启动脚本,并在launch.json的远程调试配置中的preLaunchTask下指定了所述任务。

在我的例子中,一个驻留在{workspaceRoot}中的powershell脚本在'main‘目录中编译一个名为'main’的包:

$EXECUTABLE_NAME="main“$EXECUTABLE_PATH=".\main”$GoPath=((go env | Select-String -Pattern "GOPATH=“| Out-String) -split "=")1.TrimEnd() $GoPath+="\bin”Set-Location $EXECUTABLE_PATH Start-Process go -ArgumentList 'build -gcflags "-N -l"‘-Wait -NoNewWindow #在不进行优化和内联的情况下进行编译- process ".\$EXECUTABLE_NAME.exe“$timeOut = 20 $started = $false #等待进程启动Do { Start-Sleep 250$timeOut-- $Proc = Get-Process main -ErrorAction SilentlyContinue If ($Proc) { $started = $true }} Until ($started -or $timeOut -eq 0) If (!($started)) { Write-Error 'Process not start‘Exit } $ProcId=($Proc |选择对象日志Id) Start-Process -FilePath "$GoPath\dlv.exe“-ArgumentList "attach $ProcId --headless -eq=:2345 --log”-WindowStyle Hidden

任务:

"label":"debug-attach","type":"shell","command":"powershell -ExecutionPolicy UnRestricted -File ${workspaceRoot}\debug-attach.ps1","presentation":{“debug”:“静默”,"panel":"shared","echo":false }

启动配置:

“名称”:"Attach",“类型”:"go","request":“启动”,"mode":"remote","remotePath":"${workspaceRoot}\main","port":2345,"host":"127.0.0.1","program":"${workspaceRoot}\main","preLaunchTask":"debug-attach","env":{},"args":[],"showLog":true

当我点击F5 now时,我的应用程序会弹出来,调试会自动开始,delve是隐藏的。

自2018年7月以来,HowieLiuX于2018年12月报告了following workaround

使用远程调试+ vscode任务的

task.json { //参见https://go.microsoft.com/fwlink/?LinkId=733558 //了解tasks.json格式“版本”:"2.0.0",“任务”:[{ "label":"echo","type":"shell","command":"cd ${fileDirname} && dlv debug --headless --listen=:2345 --log --api-version=2","problemMatcher":[],“组”:{ "kind":"build","isDefault":true }}]}

和:

launch.json:{ //使用IntelliSense了解可能的属性。//悬停以查看现有属性的说明。//详情请访问:https://go.microsoft.com/fwlink/?linkid=830387“版本”:"0.2.0",“配置”:[{“名称”:“连接服务器”,“类型”:“转到”,“请求”:“启动”,“模式”:“远程”,"remotePath":"${fileDirname}","port":2345,"host":"127.0.0.1","program":"${fileDirname}","env":{},"args":[] }]}

使用快捷键(在Mac中为shift+ cmd +B)运行任务: VSCode将启动一个新的shell并运行delve服务器。

按F5调试.go文件。

票数 3
EN

Stack Overflow用户

发布于 2020-04-03 15:49:01

在同一期中,HowieLiuX分享了一个适用于我的正弦波解决方案:

在tasks.json中:

代码语言:javascript
复制
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "cd ${fileDirname} && dlv debug --headless --listen=:2345 --log --api-version=2",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

但是,我不得不将命令更改为:

代码语言:javascript
复制
"command": "dlv debug --headless --listen=:2345 --log --api-version=2",

因为我运行的是powershell。

然后在launch.json中

代码语言:javascript
复制
{
    // 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": "launch",
            "mode": "remote",
            "remotePath": "${fileDirname}",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${fileDirname}",
            "env": {},
            "args": []
        }
    ]
}

首先运行任务,然后进行调试。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50884981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档