gitlab.com/Raven-IO/raven-delve@v1.22.4/Documentation/cli/getting_started.md (about)

     1  # Getting Started
     2  
     3  Delve aims to be a very simple and powerful tool, but can be confusing if you're
     4  not used to using a source level debugger in a compiled language. This document
     5  will provide all the information you need to get started debugging your Go
     6  programs.
     7  
     8  ## Debugging 'main' packages
     9  
    10  The first CLI subcommand we will explore is `debug`. This subcommand can be run
    11  without arguments if you're in the same directory as your `main` package,
    12  otherwise it optionally accepts a package path.
    13  
    14  For example given this project layout:
    15  
    16  ```
    17  github.com/me/foo
    18  ├── cmd
    19  │   └── foo
    20  │       └── main.go
    21  └── pkg
    22      └── baz
    23          ├── bar.go
    24          └── bar_test.go
    25  ```
    26  
    27  If you are in the directory `github.com/me/foo/cmd/foo` you can simply run `dlv debug`
    28  from the command line. From anywhere else, say the project root, you can simply
    29  provide the package: `dlv debug github.com/me/foo/cmd/foo`. To pass flags to your program 
    30  separate them with `--`: `dlv debug github.com/me/foo/cmd/foo -- -arg1 value`.
    31  
    32  Invoking that command will cause Delve to compile the program in a way most
    33  suitable for debugging, then it will execute and attach to the program and begin
    34  a debug session. Now, when the debug session has first started you are at the
    35  very beginning of the program's initialization. To get to someplace more useful
    36  you're going to want to set a breakpoint or two and continue execution to that
    37  point.
    38  
    39  For example, to continue execution to your program's `main` function:
    40  
    41  ```
    42  $ dlv debug github.com/me/foo/cmd/foo
    43  Type 'help' for list of commands.
    44  (dlv) break main.main
    45  Breakpoint 1 set at 0x49ecf3 for main.main() ./test.go:5
    46  (dlv) continue
    47  > main.main() ./test.go:5 (hits goroutine(1):1 total:1) (PC: 0x49ecf3)
    48       1:	package main
    49       2:	
    50       3:	import "fmt"
    51       4:	
    52  =>   5:	func main() {
    53       6:		fmt.Println("delve test")
    54       7:	}
    55  (dlv) 
    56  ```
    57  
    58  ## Debugging tests
    59  
    60  Given the same directory structure as above you can debug your code by executing
    61  your test suite. For this you can use the `dlv test` subcommand, which takes the
    62  same optional package path as `dlv debug`, and will also build the current
    63  package if not given any argument.
    64  
    65  ```
    66  $ dlv test github.com/me/foo/pkg/baz
    67  Type 'help' for list of commands.
    68  (dlv) funcs test.Test*
    69  /home/me/go/src/github.com/me/foo/pkg/baz/test.TestHi
    70  (dlv) break TestHi
    71  Breakpoint 1 set at 0x536513 for /home/me/go/src/github.com/me/foo/pkg/baz/test.TestHi() ./test_test.go:5
    72  (dlv) continue
    73  > /home/me/go/src/github.com/me/foo/pkg/baz/test.TestHi() ./bar_test.go:5 (hits goroutine(5):1 total:1) (PC: 0x536513)
    74       1:	package baz
    75       2:	
    76       3:	import "testing"
    77       4:	
    78  =>   5:	func TestHi(t *testing.T) {
    79       6:		t.Fatal("implement me!")
    80       7:	}
    81  (dlv) 
    82  ```
    83  
    84  As you can see, we began debugging the test binary, found our test function via
    85  the `funcs` command which takes a regexp to filter the list of functions, set a
    86  breakpoint and then continued execution until we hit that breakpoint.
    87  
    88  For more information on subcommands you can use, type `dlv help`, and once in a
    89  debug session you can see all of the commands available to you by typing `help`
    90  at any time.