On the importance of Debug

From junior to senior novice, apart from copying and pasting, what I feel most is the process of deubG. Through debugging, I can not only learn some contents that are not found in books, but also further improve my own level of force. With debug, you can find solutions to even weird problems.

Valid Debug mode

As a coder veteran, I thought it would be a good time to share a very useful Debug skill about GO (I learned this skill from other great powers, so I’ll just summarize it).

A proper IDE is especially important

  • For beginners or learners, I recommend GoLand here as the first development tool
  • For job development, I recommend Visual Studio Code as the first development tool

One of the things to watch out for is LiteIDE, which is a really good tool that can’t be replaced around 2013

Visual Studio Code

Advantages:

  1. Relatively lightweight
  2. Supports remote development such as SSH
  3. Multilingual development is especially convenient
  4. After installing the corresponding plug-in mode is not troublesome

Disadvantages:

  1. Development delays for Go are significant
  2. The Go module cannot be intelligently imported
  3. Unable to modulate complex Go source code

Goland

Rich people’s first choice, in addition to occupying memory, charging almost no other slot point, the key is the Debug function and its powerful.

Based on Goland source mode

The first step is to find the code entry point

Here’s a plan to find the answer from somewhere, but I don’t remember where

#Compile your code and export assembly information
go tool compile -S main.go >> main.txt
Copy the code

Open main.txt and find the following important line. If you’re smart enough, you’ll be able to spot the CALL instruction at line 7

#The main code
0x008f 00143 (main.go:7)	CALL	runtime.fastrand(SB)
Copy the code

Runtime. fastrand is the entry function for the line change code. We just need to find the corresponding function name from the Runtime package and put a breakpoint on it.

Second, use Goland to open the corresponding project in main.go

In fact, the code is very simple, which is summarized in a diagram:

If there is no way to access the breakpoint during debugging, click the button below to solve the problem

The last

The above is a little bit of Go source debugging I have mastered, I hope to see you work on behalf of a little bit of help, if there is something incorrect, or need a detailed supplement, welcome to continue to clap brick.