我正在学习golang,并尝试调试一本golang书中的示例代码。 Delve调试器未按预期工作,这很奇怪。
如您在此图中看到的,我可以在第83行设置断点,然后继续将程序运行到此断点。在左侧面板上,我可以看到显示了变量和调用堆栈。但是,当我从该断点进入下一条语句时,调试器似乎已停止。所有 变量已清除,但调用堆栈仍向我显示问题正在运行,如下图所示:
我还在命令行(外部vs-code)中尝试了delve调试器,在相同的断点处遇到了相同的问题。
VS-Code 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": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": [],
"trace": true
}
]
}
Go版本:1.10.3 Windows / amd64
Delve调试器版本:1.1.0
VS代码版本:1.29.0
我需要其他配置吗?
我在此处附加了源代码,您可以尝试看看您的计算机上发生了什么。
源代码:
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
)
func filenamesFromCmdLine() (inFilename, outFilename string, err error) {
if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
err = fmt.Errorf("Usage: %s [<]infile.txt [>]outfile.txt", filepath.Base(os.Args[0]))
return "", "", err
}
if len(os.Args) > 1 {
inFilename = os.Args[1]
}
if len(os.Args) > 2 {
outFilename = os.Args[2]
}
if inFilename != "" && inFilename == outFilename {
log.Fatal("won't overwrite the infile")
}
return inFilename, outFilename, nil
}
func americanise(inFile io.Reader, outFile io.Writer) (err error) {
reader := bufio.NewReader(inFile)
writer := bufio.NewWriter(outFile)
defer func() {
if err == nil {
err = writer.Flush()
}
}()
var replacer func(string) string
wordRx := regexp.MustCompile("[A-Za-z]+")
eof := false
for !eof {
var line string
line, err = reader.ReadString('\n')
if err == io.EOF {
err = nil
eof = true
} else if err != nil {
return err
}
line = wordRx.ReplaceAllStringFunc(line, replacer)
if _, err = writer.WriteString(line); err != nil {
return err
}
}
return nil
}
func main() {
inFilename, outFilename, err := filenamesFromCmdLine()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
//why we initialize them to stdin and stdout?
//the file object has been set to os.Stdin and os.Stdout
inFile, outFile := os.Stdin, os.Stdout
if inFilename != "" {
//os.Open() returns *os.File that can be used for reading the file.
if inFile, err = os.Open(inFilename); err != nil {
log.Fatal(err)
}
//defer inFile.Close()
}
if outFilename != "" {
//create a new file or truncate to zero length for existing file
if outFile, err = os.Create(outFilename); err != nil {
log.Fatal(err)
}
defer outFile.Close()
}
if err = americanise(inFile, outFile); err != nil {
log.Fatal(err)
}
}
答案 0 :(得分:1)
不幸的是,当您执行多个go例程(每个例程都等待进入下一步)时,VS-Code中的Delve无法很好地处理“ GoTo Next Step”。我使用的一种解决方法是,将另一个调试点放在要停止调试的位置,然后选择“继续”选项。
答案 1 :(得分:0)
我不确定您的情况,但是vscode-go以前的版本中存在错误。您的版本是0.7.0吗?