{
"path": "/posts/2018/2018-07-23-go-debugging-with-delve",
"site": "at://did:plc:mracrip6qu3vw46nbewg44sm/site.standard.publication/self",
"tags": [
"code",
"go",
"delve"
],
"$type": "site.standard.document",
"title": "Debugging go code with delve",
"updatedAt": "2018-07-23T20:10:00.000Z",
"publishedAt": "2018-07-23T20:10:00.000Z",
"textContent": "> Delve is a debugger for the Go programming language. The goal of the project is to provide a simple, full featured debugging tool for Go.\n\nIf we run our go service using a Makefile, with a command like make run, it can hard to find where to hook in and call dlv debug. We can get around this issue by attaching the delve debugger to our running service instead. First set a breakpoint in the code, on the code path you intend to trigger by adding the statement runtime.Breakpoint(). Don't forget to import the runtime package.\n\nNow, in one window run:\n\nIn another, run:\n\nmygoproj is the name of our service. The above command grabs our process id and hooks the delve debugger up to it.\n\nNow that we're in the debugger, type c for continue. If your breakpoint is in the main thread, delve will jump to it. If you need to make an external call to trigger the codepath, go ahead and do that now (with curl otherwise) and delve will jump to your breakpoint.\n\nConsider the following code:\n\nWe trigger the codepath with yab:\n\nDelve drops us into the code at the breakpoint:\n\nWe can now explore what's going on in the program. Typing help will show everything delve can do. I use args and locals to see the variables that exist within the function containing the breakpoint:\n\nIf we want to see the value of a variable we can print it (p for short):\n\nWe can also drill down into structs using dotted paths:\n\nWe can even cast values:\n\nTo allow the program to continue, type c or continue again. To exit the debugger, type exit or Ctrl-d.\n\nThe nice part about using delve this way is it doesn't have a dependence on any one IDE or editor. Happy hacking.",
"canonicalUrl": "https://www.danielcorin.com/posts/2018/2018-07-23-go-debugging-with-delve"
}