Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of debugging Golang Project in VSCode

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report