Go Language Practical Notes (23) | Go debugging

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

"Go language Combat" reading notes, not to be continued, welcome to sweep code attention flysnow_org to the public or website http://www.flysnow.org/, the first time to see follow-up notes. If you feel helpful, share it with your friends and thank you for your support.

For any programmer, debugging a program is a must-have skill. When we develop a good program that does not meet our expectations, we need to debug it to find the root cause, before we can target to solve it.

Debugging is a skill, not limited to what we call debug, this is just one of them, this is the debugging that can break the point, in addition, there are printouts, logging, unit testing, which can be called debugging program means.

Print output

PrintOut is a more traditional debugging method, we can print out the variable values we need to understand, the steps we perform, and so on, to prove our guess in order to solve the problem.

123456789
func min  int) int  {//debug Output fmt. Println ("A:", A,", B:", b)if a>b {returnelse {return A}}

This is a function to find the minimum value, in which fmt.Println("a:",a,",b:",b) we add the debug output statement, so that we can see the value of the two number of comparisons, and then speculate that our algorithm to find the minimum value is correct.

The printout can not only output numerical values, but also can test whether there is output to determine which logic the program went through, such as if statements, possibly because of the error of comparison, the program entered another if branch, then we can use the printout to determine.

Log output

This is very much like printing, but he has a lot more information than printing, such as the time of occurrence, the number of lines of code, etc., which is especially useful when we are having problems with the line.

For example, we monitor some of the user's login has failed, then we can use the log in our log file to locate the time, IP, user, error message and line of code, and finally find the problem, solve the problem. About the application of the log can refer to my previous article http://www.flysnow.org/2017/05/06/go-in-action-go-log.html, here is no longer described in detail.

GDB debugging

GDB is a class of Unix-like debugger, can help us debug executable programs, go also to gdb support, so we can also apply GDB debugging Go program.

GDB is the command line tool, before debugging, we have to compile the program we need to debug. Use go build -gcflags "-N -l" main.go to compile. -N -lthe markup here is to ignore the meaning of compiler optimizations, so that we can debug the program more easily.

Debugging through the gdb main start, will enter a GDB debugging interface, in which we can view the list code through the command, you can break the point by the command b to a line, you can run through the run command, and so on, the specific can refer to GDB related documents, here do not do a detailed introduction.

Delve commissioning

Delve is a debugging tool specifically designed to debug go programs, which is more powerful than GDB, especially when debugging a goroutine high-concurrency Go program. Delve's project address is https://github.com/derekparker/delve, and its performance is most of the debugging tools that go development IDE chooses, such as Goland, which we'll cover later.

The use of delve and gdb is very much like, is the command line tool, we can dlv debug main.go start, then we will enter an interface, in this interface we can debug.

Some common commands for delve are as follows:

1.break Break Point

    1. Continue run to Breakpoint
    2. Clear Delete Breakpoint
    3. List Display source code
    4. Next runs to the next line
    5. Locals printing local variables
    6. Print prints a variable or expression
    7. Exit Debug

In addition, there are many, you can refer to delve's documentation for debugging.

IDE Debugging

For debugging, the command line is too cumbersome, so in order to tool efficiency, we recommend that you use the IDE for debugging, visible, progressive, high efficiency.
Debugging this thing, as long as we can solve our problems, there is a good IDE can help us more efficient debugging, he le it.

Here recommended Goland,jetbrains produced, veteran IDE manufacturers, very convenient. It has a lot of IDE, and the shortcut keys are the same, will have a other also will.

IDE debugging is very simple, click on the toolbar's Start Debugging button, you can debug, remember to hit the breakpoint in advance. Then F8 and F7 coordination can be debugged, there is a dedicated debugging window can see the current local variables, stack information and so on, very simple, here do not do a detailed introduction.

Unit Test

Unit testing is also a means of debugging, we can simulate a lot of scenarios to verify the correctness of our logic, encountered problems, print output, etc., so is also a means of debugging, specific to go unit test can refer to http://www.flysnow.org/2017/05/16/ Go-in-action-go-unit-test.html This article, which is no longer described in detail here.

Debugging is a means, but also a skill, to solve the problem for the purpose, as long as the problem can be solved, is a good debugging means.

"Go language Combat" reading notes, not to be continued, welcome to sweep code attention flysnow_org to the public or website http://www.flysnow.org/, the first time to see follow-up notes. If you feel helpful, share it with your friends and thank you for your support.

Related Article

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.