使用 delve 调试 Debug Golang 程序

发布时间:阅读数:84


在 Vscode 开发程序中,需要调试Debug程序的功能,对程序进行分析。Vscode 是通过 dlv 来进行实现的,本次就是刚好根据实现,做一个简单的检查

帮助文档

 -test.bench regexp
    	run only benchmarks matching regexp
  -test.benchmem
    	print memory allocations for benchmarks
  -test.benchtime d
    	run each benchmark for duration d (default 1s)
  -test.blockprofile file
    	write a goroutine blocking profile to file
  -test.blockprofilerate rate
    	set blocking profile rate (see runtime.SetBlockProfileRate) (default 1)
  -test.count n
    	run tests and benchmarks n times (default 1)
  -test.coverprofile file
    	write a coverage profile to file
  -test.cpu list
    	comma-separated list of cpu counts to run each test with
  -test.cpuprofile file
    	write a cpu profile to file
  -test.failfast
    	do not start new tests after the first test failure
  -test.list regexp
    	list tests, examples, and benchmarks matching regexp then exit
  -test.memprofile file
    	write an allocation profile to file
  -test.memprofilerate rate
    	set memory allocation profiling rate (see runtime.MemProfileRate)
  -test.mutexprofile string
    	write a mutex contention profile to the named file after execution
  -test.mutexprofilefraction int
    	if >= 0, calls runtime.SetMutexProfileFraction() (default 1)
  -test.outputdir dir
    	write profiles to dir
  -test.paniconexit0
    	panic on call to os.Exit(0)
  -test.parallel n
    	run at most n tests in parallel (default 16)
  -test.run regexp
    	run only tests and examples matching regexp
  -test.short
    	run smaller test suite to save time
  -test.shuffle string
    	randomize the execution order of tests and benchmarks (default "off")
  -test.testlogfile file
    	write test action log to file (for use only by cmd/go)
  -test.timeout d
    	panic test binary after duration d (default 0, timeout disabled)
  -test.trace file
    	write an execution trace to file
  -test.v
    	verbose: print additional output

Vscode启动程序配置

和单元测试一样,

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "args": ["-test.run", "TestTwo"],
            "program": "${file}"
            
        }
    ]
}

代码文件

package debugt

import (
	"log"
	"testing"
)

func TestOne(t *testing.T) {
	a := 10
	log.Print(a)
}

func TestTwo(t *testing.T) {
	b := 5
	log.Print(b)
}

提示

-test.run 可以按照匹配的方式进行调试

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "args": ["-test.run", "(TestTwo|TestOne)"],
            "program": "${workspaceFolder}/debug_test.go"
            
        }
    ]
}

参看文档

  1. https://www.bookstack.cn/read/vscode-go/debugging.md

  2. https://github.com/go-delve/delve/blob/master/Documentation/usage/dlv.md


置顶文章
推荐文章