Tips for debugging Golang programs with delve

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Want to know more, add QQ group 72132378

Google's official debug example for Golang uses GDB by default

However, using GDB to debug go programs will encounter goroutine problems, because GDB does not understand the go

Therefore, delve black technology is used to debug the Golang program.

The Pure command line debugging method is easy to search on the net, this article mainly takes the liteide to carry on the program debugging

Turn off compiler optimizations

Normal go build/install out of the GO program is fully optimized, forced to use the debugger hook debugging, some local variables/lamda expression captured variables will go directly into the register, cannot use the debugger to view

Delete all pkg, add the parameter-gcflags "-n-l" to the build or install parameter to turn off compiler optimization

For example:

    1. go install -gcflags "-N -l" svc\gamesvc

Delve Debugger Installation Method

Liteide comes with GDB, but does not have a delve debugger, it needs to install itself, the command is as follows

    1. go get github.com/derekparker/delve/cmd/dlv

The delve debugger will be placed under your gopath/bin.

Delve debugger configuration in Liteide

Select Debugger

In the Liteide menu, select Debug->debugger/delve

Delve environment variable settings

At this time, Liteide still cannot find delve, because it is not in the environment variable path, there is no need to modify environment variables, only need to liteide environment configuration

In the Liteide menu, select View--Edit current environment, modify in Popup document

    1. PATH=c:\mingw32\bin;%GOROOT%\bin;%PATH%;c:\your\path\to\delve

Remove the comment # before path, add a semicolon in%path%, and then the path to the delve debugger

Start debugging

Select your project, click F5, enter debug mode

Debugger Displays variable values

Liteide when debugging with delve, it is not possible to properly capture delve debug return data in Windows such as variable monitoring (because the format is too complex ...)

It doesn't matter, we can use the command line to display

Liteide console or debug Output window is actually a standard command line when delve debugging

The command is as follows

  • P variable name to view variable values

  • Locals viewing local variables

  • LS to view the current file

  • Stack View Stack

  • Help can see a variety of helpful

Debugging external Programs

If your program is an external program, or a program installed to the Gopath/bin directory using go install, you may encounter a startup path error when you start the program using the delve debugger

debugging other applications using liteide menu debugging ... Fill in the path you want to debug the program and working directory, you can solve this problem


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.