【问题标题】:How can I simulate interactive console in VSCode debugger?如何在 VSCode 调试器中模拟交互式控制台?
【发布时间】:2019-08-26 02:02:16
【问题描述】:

我正在尝试调试这个 Go 程序,它读取文本并通过 VSCode 调试器将其输出到控制台。

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    text, _ := reader.ReadString('\n')
    fmt.Println(text)
}

它在终端上运行良好,但是当我使用 VSCode 调试它时,即使我专注于调试输出,我也无法输入任何内容。

调试部分有一个控制台,但它是 REPL 评估器,因此它也与终端控制台无关。

如何在 VSCode 中启用控制台,以便在程序中键入文本?

【问题讨论】:

    标签: go visual-studio-code vscode-debugger delve


    【解决方案1】:

    这后面是Microsoft/vscode-go issue 219,并且仍然打开。

    是的 - VS Code 调试器控制台目前不支持将任何输入通过管道传输到标准输入。

    仅供参考,您可以告诉代码调试器在启动配置中使用外部控制台:

    "externalConsole": true
    

    它有一个possible workaround,使用远程调试+ vscode 任务,但这不是微不足道的。

    tasks.json:

    {
        // 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
                }
            }
        ]
    }
    

    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": "launch",
                "mode": "remote",
                "remotePath": "${fileDirname}",
                "port": 2345,
                "host": "127.0.0.1",
                "program": "${fileDirname}",
                "env": {},
                "args": []
            }
        ]
    }
    

    使用快捷键(command/control + shift + B)运行任务,vscode会启动一个新的shell并运行delve服务器。

    按 F5 调试 .go 文件。对我来说效果很好。

    【讨论】:

      【解决方案2】:

      我使用这个解决方法解决了这个问题 - https://github.com/golang/vscode-go/issues/124#issuecomment-999064812

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 2018-12-18
      • 2023-04-01
      相关资源
      最近更新 更多