obj*_*ect 14 visual-studio-code
I want to debug a Go program in Visual Studio Code 1.24.0 which goes like this:
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World")
var input int
fmt.Scanf("%d", &input)
fmt.Printf("Hello %v", input)
}
Run Code Online (Sandbox Code Playgroud)
When start debugging, the program waits for input. I tried giving input via Debug Console, but it didn't work. Properties like externalConsole don't seem to work in launch.json for Go. Any inputs?
Win*_*and 44
现在有一个简单的解决方案。您可以将以下行附加到启动配置中:
"console": "integratedTerminal"
Run Code Online (Sandbox Code Playgroud)
我很高兴与大家分享,找到了一个更可用的解决方案(它在启动之前自动运行任务)。
...
启动.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}"
}
]
}
Run Code Online (Sandbox Code Playgroud)
和
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.*"
}
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
它期望您正在使用 go.mod,因为 delve 也会查找它来调试目录。
这类似于vscode-go 问题 219
解决方法(由 提出KingRikkie)是:
不过,有一个解决方法。我编写了一个执行以下操作的脚本:
然后,我在 VScode 中创建了一个新任务,该任务启动脚本并
preLaunchTask在launch.json.就我而言,powershell 脚本位于编译 ' ' 目录中
{workspaceRoot}名为 ' ' 的包中:mainmainRun Code Online (Sandbox Code Playgroud)$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 # compile without optimizations and inlining Start-Process ".\$EXECUTABLE_NAME.exe" $timeOut = 20 $started = $false # wait for process to start Do { Start-Sleep -Milliseconds 250 $timeOut-- $Proc = Get-Process main -ErrorAction SilentlyContinue If ($Proc) { $started = $true } } Until ($started -or $timeOut -eq 0) If (!($started)) { Write-Error 'Process did not start' Exit } $ProcId=($Proc | Select-Object -expand Id) Start-Process -FilePath "$GoPath\dlv.exe" -ArgumentList "attach $ProcId --headless --listen=:2345 --log" -WindowStyle Hidden任务:
Run Code Online (Sandbox Code Playgroud)"label": "debug-attach", "type": "shell", "command": "powershell -ExecutionPolicy UnRestricted -File ${workspaceRoot}\\debug-attach.ps1", "presentation": { "reveal": "silent", "panel": "shared", "echo": false }启动配置:
Run Code Online (Sandbox Code Playgroud)"name": "Attach", "type": "go", "request": "launch", "mode": "remote", "remotePath": "${workspaceRoot}\\main", "port": 2345, "host": "127.0.0.1", "program": "${workspaceRoot}\\main", "preLaunchTask": "debug-attach", "env": {}, "args": [], "showLog": true现在,当我按 F5 时,我的应用程序将弹出,调试将自动开始,delve 被隐藏。
自 2018 年 7 月以来,HowieLiuX于 2018 年 12 月报告了以下解决方法
使用远程调试+ vscode 任务:
Run Code Online (Sandbox Code Playgroud)task.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 } } ] }
和:
Run Code Online (Sandbox Code Playgroud)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": [] } ] }使用快捷键(Mac OS 中为 Shift + cmd + B)运行任务:VSCode 将启动一个新 shell 并运行 delve 服务器。
按 F5 调试.go文件。
| 归档时间: |
|
| 查看次数: |
4903 次 |
| 最近记录: |