Golang delve: Difference between revisions

From wikinotes
No edit summary
Line 27: Line 27:
Delve builds your executable automatically with flags required for it to debug your program.
Delve builds your executable automatically with flags required for it to debug your program.


== Start Delve ==
<blockquote>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# start debugger
# start debugger
Line 32: Line 34:
dlv test  [example.com/x/foo/package]                      # run tests
dlv test  [example.com/x/foo/package]                      # run tests
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Start Delve -->
== Breakpoint ==
<blockquote>
You can add breakpoints in src, or interactively


Add your breakpoints, then continue to execute your program
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
runtime.Breakpoint() // delve breakpoint
</syntaxhighlight>
<syntaxhighlight lang="bash">
b main.main  # add breakpoint to 'main.go's 'main' method
b main.main  # add breakpoint to 'main.go's 'main' method
b main.go:10  # add breakpoint to 'main.go's line 10
b main.go:10  # add breakpoint to 'main.go's line 10
</syntaxhighlight>
</blockquote><!-- Breakpoint -->


== Session ==
<blockquote>
Add your breakpoints, then continue to execute your program
<syntaxhighlight lang="go">
c            # continue (start executing)
c            # continue (start executing)
</syntaxhighlight>
</syntaxhighlight>
Line 49: Line 65:
<enter>    # repeat last command
<enter>    # repeat last command
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Session -->
</blockquote><!-- Usage -->
</blockquote><!-- Usage -->

Revision as of 18:20, 26 June 2022

delve is a go debugger.
It is more knowledgeable about the go runtime than golang gdb .
There are also editor integrations.

Documentation

github https://github.com/go-delve/delve
command docs https://github.com/go-delve/delve/blob/master/Documentation/cli/README.md
editor plugins https://github.com/go-delve/delve/blob/master/Documentation/EditorIntegration.md

Install

go install github.com/go-delve/delve/cmd/dlv@latest

Usage

Delve builds your executable automatically with flags required for it to debug your program.

Start Delve

# start debugger
dlv debug [example.com/x/foo/package] [-- [--your-params]]  # run program
dlv test  [example.com/x/foo/package]                       # run tests

Breakpoint

You can add breakpoints in src, or interactively

runtime.Breakpoint() // delve breakpoint
b main.main   # add breakpoint to 'main.go's 'main' method
b main.go:10  # add breakpoint to 'main.go's line 10

Session

Add your breakpoints, then continue to execute your program

c             # continue (start executing)

Inspect your program, learn

p yourVar  # print var
l          # print location
n          # next line (enters loop, not skip to end)
s          # step into line
<enter>    # repeat last command