preface
During the development or troubleshooting process, you may encounter some bugs that can be repeated only in the official environment. However, because the official environment cannot be accessed locally, you can only Debug problems in limited ways, such as adding logs, which is inefficient. Therefore, it is recommended to give up this way and use IDE and Delve for remote debugging to complete the rapid investigation and repair of bugs.
To prepare
-
A server with access to the formal environment.
-
IDE, Jetbrains IDE is recommended. GoLand is used as an example.
steps
1. Install Delve
Install Delve on the server.
Website: https://github.com/derekparker/delve, installation method, please refer to the official wiki.
2. Check the IDE instructions
Compile and start the program as much as possible according to the IDE instructions. Click the menu bar Run -> EditConfigurations -> + -> GoRemote in GoLand. We should see the following screen. In the middle are compilation and startup instructions.
3. Compile your program
According to the IDE instructions, to compile your program on the server, you must add the -gcFlags parameter, and the rest is optional.
-
Go 1.10 and above
go build -gcflags "all=-N -l" github.com/app/demo
Copy the code
-
Go 1.9 and below
go build -gcflags "-N -l" github.com/app/demo
Copy the code
4. Start your program with DLV
Use DLV to start the compiled program on the server according to the IDE instructions.
dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./demo
Copy the code
If the program requires a startup parameter, add — after it.
dlv --listen=: 2345 -- headless=true --api -version= 2 -- accept-multiclient exec ./ demo -- - c=/config
# is equal to
./ demo -c =/config
Copy the code
5. Configure debugging locally
Add GoRemote in your EditConfigurations and configure the server IP address and Port number in Host and Port.
Once configured, we can enjoy remote Debug as well as local Debug.
Resources: the language remote debugging method [https://linkscue.com/2018/09/07/2018-08-29-golang-remote-debug/]