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