go - 如何在 VSCode 调试器中模拟交互式控制台?

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

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

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 中启用控制台以便我可以向程序键入文本?

最佳答案

接下来是 Microsoft/vscode-go issue 219 , 并且仍然打开。

Yes - the VS Code debugger console doesn't currently support piping any input through to stdin.

FYI, you can tell the Code debugger to use an external console in the launch config:

"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": []
        }
    ]
}

Run the task using shortcut key(command/control + shift + B), vscode will start a new shell and run the delve server.

Press F5 to debug the .go file. It works fine for me.

关于go - 如何在 VSCode 调试器中模拟交互式控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57651058/

相关文章:

arrays - 在 golang 中展平递归数据结构的有效方法

node.js - 用于启动前端和后端进行调试的 VS Code 设置

visual-studio-code - 使用 Visual Studio Code 进行调试并将终端输出管道传输到文件

Visual Studio 可以用来运行带有 ARM 脚本的 C 代码吗?

javascript - 期望不正确的智能感知方法

ubuntu - 如何在 Ubuntu -2021 上更新 VSCode

javascript - VS Code 中的绿色调试器指针是什么意思?

go - 转换 slice 的字节而不复制

go - 在一个变量中存储 2 个值

java - Golang enum 可以像 Java 的 enum 一样做同样的行为吗?