我正在学习 golang 并尝试调试来自我的一本 golang 书的示例代码。很奇怪,delve 调试器没有按预期工作。
正如你在这张图片中看到的,我可以在第 83 行设置断点并继续运行程序到这个断点。在左侧面板上,我可以看到显示了变量和调用堆栈。但是当我从那个断点进入下一条语句时,调试器似乎停止了。所有变量都被清除,但调用堆栈仍然显示问题正在运行,如下图所示:
我还在命令行中尝试了 delve 调试器(外部 vs-code),我在同一个断点上遇到了同样的问题。
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)
}
}