delve是一款专门针对Golang程序调试而开发的命令行调试器,该工具功能强大,简单易用。
本文从安装开始,使用一个实际的Golang程序调试例子,学习一下delve的主要调试方式及常用调试命令。
本文所使用的是Windows环境。
1 安装
使用go get命令安装构建delve。
$ go get -u github.com/go-delve/delve/cmd/dlv
其会在$GOPATH/bin下生成二进制可执行文件dlv.exe,将$GOPATH/bin添加到PATH环境变量即可在任意目录使用dlv。
2 主要调试方式
键入dlv help可以看到dlv的使用帮助文档。
$ dlv helpDelve is a source level debugger for Go programs.Delve enables you to interact with your program by controlling the execution of the process,evaluating variables, and providing information of thread / goroutine state, CPU register state and more.The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.Pass flags to the program you are debugging using `--`, for example:`dlv exec ./hello -- server --config conf/config.toml`Usage: dlv [command]Available Commands: attach Attach to running process and begin debugging. connect Connect to a headless debug server. core Examine a core dump. debug Compile and begin debugging main package in current directory, or the package specified. exec Execute a precompiled binary, and begin a debug session. help Help about any command run Deprecated command. Use 'debug' instead. test Compile test binary and begin debugging program. trace Compile and begin tracing program. version Prints version.Flags: ...
下面我们会结合一个实际的例子看一下主要的几种调试方式。
我们就以Golang官方的例子github.com/golang/example/hello着手吧。
$ go get -u github.com/golang/example/hello
如上命令会将github.com/golan