gpt4 book ai didi

visual-studio-code - 在 Visual Studio Code 中调试 GO 时如何读取输入?

转载 作者:行者123 更新时间:2023-12-03 17:29:49 28 4
gpt4 key购买 nike

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

package main

import (
"fmt"
)

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

开始调试时,程序等待输入。我尝试通过调试控制台提供输入,但没有奏效。像 externalConsole 这样的属性在用于 Go 的 launch.json 中似乎不起作用。任何输入?

最佳答案

在同一期 HowieLiuX 分享了一个对我有用的更简单的解决方案:

在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
}
}
]
}

但是,我不得不将命令更改为:
"command": "dlv debug --headless --listen=:2345 --log --api-version=2",

因为我正在运行powershell。

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

首先运行任务然后调试。

关于visual-studio-code - 在 Visual Studio Code 中调试 GO 时如何读取输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50884981/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com