Golang调试工具Delve安装及使用

栏目: Go · 发布时间: 6年前

内容简介:Golang debug 推荐使用 Delve 工具,项目地址:一、安装照着 github 上 delve 项目的安装说明操作,

Golang debug 推荐使用 Delve 工具,项目地址: https://github.com/derekparker/delve

一、安装

照着 github 上 delve 项目的安装说明操作, go mod 模式下推荐使用第二种方式。

1.拉取最新 delve 项目代码到本地,编译安装。

# cd $GOPATH/src/
# git clone https://github.com/derekparker/delve.git
# cd delve/cmd/dlv/
# go build
# go install

国内环境 go build 会报错:

go: golang.org/x/crypto@v0.0.0-20180614174826-fd5f17ee7299: unrecognized import path "golang.org/x/crypto" (https fetch: Get https://golang.org/x/crypto?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/sys@v0.0.0-20180614134839-8883426083c0: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
go: golang.org/x/arch@v0.0.0-20171004143515-077ac972c2e4: unrecognized import path "golang.org/x/arch" (https fetch: Get https://golang.org/x/arch?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

原因是 Golang 官网被墙了,这里手动修改 go.mod 文件,把项目地址替换为 github 上的地址,如:

# vim ../../go.mod
# 添加下面替换:
replace (
        golang.org/x/arch v0.0.0-20171004143515-077ac972c2e4 => github.com/golang/arch v0.0.0-20171004143515-077ac972c2e4
        golang.org/x/crypto v0.0.0-20180614174826-fd5f17ee7299 => github.com/golang/crypto v0.0.0-20180614174826-fd5f17ee7299
        golang.org/x/sys v0.0.0-20180614134839-8883426083c0 => github.com/golang/sys v0.0.0-20180614134839-8883426083c0
)

如图:

Golang调试工具Delve安装及使用

image.png

然后重新编译安装,没有报错则成功。

2.添加 $GOPATH/bin 到环境变量,执行 dlv 命令,查看:

Golang调试工具Delve安装及使用

image.png

二、使用 Delve 调试程序

1.查看 Delve 支持命令: dlvdlv --help ,如下:

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.

2.查询单个命令详情, dlv [command] --help ,如: dlv debug --help

3.调试程序

手动创建一个 helloworld 项目, main.go 里面打印一些信息,如:

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

执行 debug 调试:

# cd helloword/
# go mod init
# dlv debug main.go

开启调试成功,执行 help 查看 debug 命令:

Golang调试工具Delve安装及使用

image.png

PS:

因为开启了 go mod 模式,所以 debug 前这里需要 go mod init 初始化 mod,不然会报错:

go: cannot find main module; see 'go help modules'

4.其他命令参数,自行调试。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

函数式算法设计珠玑

函数式算法设计珠玑

Richard Bird / 苏统华、孙芳媛、郝文超、徐琴 / 机械工业出版社 / 2017-4-1 / 69.00

本书采用完全崭新的方式介绍算法设计。全书由30个珠玑构成,每个珠玑单独列为一章,用于解决一个特定编程问题。这些问题的出处五花八门,有的来自游戏或拼图,有的是有趣的组合任务,还有的是散落于数据压缩及字串匹配等领域的更为熟悉的算法。每个珠玑以使用函数式编程语言Haskell对问题进行描述作为开始,每个解答均是诉诸于函数式编程法则从问题表述中计算得到。本书适用于那些喜欢学习算法设计思想的函数式编程人员、......一起来看看 《函数式算法设计珠玑》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具