100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > delve应该安装到哪_Golang调试工具Delve安装及使用

delve应该安装到哪_Golang调试工具Delve安装及使用

时间:2019-06-11 10:51:38

相关推荐

delve应该安装到哪_Golang调试工具Delve安装及使用

一、安装

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

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

# cd $GOPATH/src/

# git clone /derekparker/delve.git

# cd delve/cmd/dlv/

# go build

# go install

国内环境 go build 会报错:

go: /x/crypto@v0.0.0-0614174826-fd5f17ee7299: unrecognized import path "/x/crypto" (https fetch: Get /x/crypto?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

go: /x/sys@v0.0.0-0614134839-8883426083c0: unrecognized import path "/x/sys" (https fetch: Get /x/sys?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

go: /x/arch@v0.0.0-1004143515-077ac972c2e4: unrecognized import path "/x/arch" (https fetch: Get /x/arch?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

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

# vim ../../go.mod

# 添加下面替换:

replace (

/x/arch v0.0.0-1004143515-077ac972c2e4 => /golang/arch v0.0.0-1004143515-077ac972c2e4

/x/crypto v0.0.0-0614174826-fd5f17ee7299 => /golang/crypto v0.0.0-0614174826-fd5f17ee7299

/x/sys v0.0.0-0614134839-8883426083c0 => /golang/sys v0.0.0-0614134839-8883426083c0

)

如图:

image.png

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

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

image.png

二、使用 Delve 调试程序

1.查看 Delve 支持命令: dlv 或 dlv --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 命令:

image.png

PS:

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。