Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The method of debugging Golang Project in VSCode

Shulou Source: shulou.com Published: 2022-06-02 12:56:52 11月30日 Update

Editor to share with you how to debug the Golang project in VSCode. I hope you will get something after reading this article. Let's discuss it together.

Keyword

The simplest debugging strategy

Multi-project debugging, suitable for personal development and project development

No need to modify system environment variables

Prepare VSCode

Download the latest version of VSCode on the official website:

Visual Studio Code-Code Editing. Redefined**

Install the Golang plug-in

Open the expansion panel VSCode- > View-> expand

Find the Go plug-in, type Go in the search box, find the plug-in with Rich Go language support for Visual Studio Code on the second line, and click install note that it is not the highest ranking.

Restart the editor

Configure Startup item

Open debug panel VSCode- > View-> Debug

Add debug target click "add configuration.." in the drop-down box of "No debugging".

Add target debug configuration

Example:

{"version": "0.2.0", "configurations": [{"name": "Launch", "type": "go", "request": "launch", "mode": "debug", "remotePath": "," port ": 2345 "host": "127.0.0.1", "program": "${fileDirname}", "env": {"GOPATH": "D:/Develop/vscodegolang"}, "args": [], "showLog": true}]}

Among them: "port" and "host" are automatically generated by the go plug-in

"env" to set the environment variable, set it to your project directory (folder containing bin and src)

Prepare to debug the plug-in

If you find main.go and press F5, you will get an error message:

Failded to continue: "Cannot find Delve debugger. Install from https://github.com/derekparker/delve & ensure it is in your" GOPATH/bin "or" PATH "

We use the go command line to compile the debugger

Go get github.com/derekparker/delve/cmd/dlv

Place the dlv debugger in the bin directory of GOPATH (project directory)

Start debugging

Select the main.go you want to debug and click F5 to start debugging.

Debug shortcuts are consistent with Visual Studio.

F9 switching breakpoint

F10 Step over

F11 Step in

Shift+F11 Step out

Pay attention

When some structure members cannot be displayed directly, you can directly select the variable name and add it to the monitoring, or right-click: "Debug: evaluation"

Multi-project debugging

Multiple groups of debugging entries can be added in launch.json, and debugging of different targets can be enabled by selecting the corresponding configuration in the debugging panel.

{"version": "0.2.0", "configurations": [{"name": "client", "type": "go", "request": "launch", "mode": "debug", "remotePath": "," port ": 2345 "host": "127.0.0.1", "program": "${fileDirname}", "env": {"GOPATH": "D:/Develop/vscodegolang"}, "args": [], "showLog": true}, {"name": "server" "type": "go", "request": "launch", "mode": "debug", "remotePath": "", "port": 2345, "host": "127.0.0.1", "program": "${workspaceRoot} / src/server" "env": {"GOPATH": "D:/Develop/vscodegolang"}, "args": [], "showLog": true}]}

The "${fileDirname}" in "program" uses the currently selected file as the starting point

After reading this article, I believe you have a certain understanding of "the method of debugging Golang Project in VSCode". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Plug-ins projects configurations variables directories goals panels projects methods files environment articles debuggers preparation development different maximum consistent examples portals Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi Redmi Shulou Tech Info Linux