使用vscode无法正常对golang程序进行debug

运行环境

这是我的vscode的一些信息
版本: 1.58.2
提交: c3f126316369cd610563c75b1b1725e0679adfb3
日期: 2021-07-14T22:30:16.440Z
Electron: 12.0.13
Chrome: 89.0.4389.128
Node.js: 14.16.0
V8: 8.9.255.25-electron.0
OS: Linux x64 5.10.0-kali9-amd64

情况的概述

在vscode使用F5启动调试,在执行到类似,fmt.scanf(),str<-chan1之类的语句时,右下角会出现这样的错误提示

使用vscode无法正常对golang程序进行debug

也许是因为go语言的堆栈可能会动态变化

会出现问题的代码

一下代码在使用go run命令运行时都是正常运行的,但是无法使用F5调试

package main

import (
    "fmt"
    "time"
)

var c chan string

func foo() {
    for {
        c <- "hello"//运行这句话时出现错误
    }
}

func main() {
    c = make(chan string)
    go foo()
    time.Sleep(10 * time.Second)
    s := <-c
    fmt.Println(s)
}
package main

import (

"fmt"

)

func  main() {

var  str  string

fmt.Scanln(&str)//运行这句话是出现错误

fmt.Print(str)

}

我的困难

我希望调试使用到这些语句的程序,但是程序一旦调试到这些语句的时候出现的错误会导致我无法调试下面的语句。我如何才能调试使用到这些语句的程序呢?

我尝试过的解决方案

1)使用dlv
我的bin目录下是含有的dlv的,但是我不知道应该对vscode做哪些设置,才能正常的使用dlv。网上的一些launch.json的配置方式,我使用了,但是没有效果。

讨论数量: 7
roseduan

初学用 Goland 吧

2年前 评论

...我的vscode就没怎么配置,直接可以debug。

{
    // 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": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}"
        }
    ]
}

不过,你还可以选择goland去用啊,没必要耗在vscode上,有些时候一些配置问题能消耗完你的耐心。

另外你的代码错误在你创建通道的方式不对,只能用make去创建。不能用var,var创建的chan的value是nil。A nil channel is never ready for communication.

channel

The capacity, in number of elements, sets the size of the buffer in the channel. If the capacity is zero or absent, the channel is unbuffered and communication succeeds only when both a sender and receiver are ready. Otherwise, the channel is buffered and communication succeeds without blocking if the buffer is not full (sends) or not empty (receives). A nil channel is never ready for communication.

2年前 评论
runstone (作者) 2年前
kelikeli (楼主) 2年前

关于无法跟踪堆栈的问题已解决。 解决过程,我使用dpkg --purge code命令删除了vscode及其配置文件。然后重新去官网下载了最新版,之后就可以正常使用chan了。注:这只是我一个猜测,在vscode安装的时候,会根据原有的一些软件设置。所以在第二次安装时,我的系统中是安装了vscode推荐的全套golang第三方库的。 但是我依旧无法正常debug标准输入语句: 在运行至fmt.scanln()时,debug console中无法输入我想要输入的东西。假如我试着输入,那么就会看到 Unable to process `evaluate`: debuggee is running

2年前 评论

关于vscode调试golang不能在调试控制台中输入的问题也得到了解决。方案来自于这篇博客blog.csdn.net/weixin_42529589/arti...

2年前 评论

问题方式可以参考github的这个issue,官方明确说明调试不是用来进行stdin标准输入的,官方给出的解决方式是采取本地远程调试的方式,让你在终端内可以输入字符用于调试 github.com/golang/vscode-go/issues...

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!