Home > Development Tools > VSCode > Detailed explanation of how to debug Golang projects in VSCode

Detailed explanation of how to debug Golang projects in VSCode

青灯夜游
Release: 2021-04-13 19:02:56
forward
5393 people have browsed it

This article will introduce to you how to use VSCode to debug Golang projects. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of how to debug Golang projects in VSCode

## Recommended study: "

vscode tutorial"

Keywords

    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 from the official website:

Visual Studio Code - Code Editing. Redefined**

Install the Golang plug-in

    Open the extension panel VSCode->View->Extension
  • Find the Go plug-in. Enter Go in the search box, find the plug-in with Rich Go language support for Visual Studio Code written on the second line, and click to install. Note that it is not the highest ranked
  • Restart the editor
Configure startup items

    Open the debugging panel VSCode->View->Debug
  • Add debugging target Click "Add configuration.." in the drop-down box of "No debugging"
  • Add target debugging 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         }     ] }
Copy after login

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

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

Prepare debugging plug-in

At this time, find main.go and press F5, an error message will be reported:

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

We use the go command line to compile the debugger

go get github.com/derekparker/delve/cmd/dlv
Copy after login

Debug the dlv Place the tool in the bin directory of GOPATH (project directory)

Start debugging

Select main.go to be debugged, click F5, you can start debugging

Debugging shortcut keys Consistent with Visual Studio system

    F9 Switch breakpoint
  • F10 Step over
  • F11 Step in
  • Shift F11 Step out
Notes

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

You can add multiple sets of debugging entries in launch.json, and select the corresponding configuration in the debugging panel to enable debugging for different targets

{
    "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
        }
    ]
}
Copy after login

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

It is more recommended to use the "${workspaceRoot}" of "program" and configure it with the package name as the starting point

For more programming-related knowledge, please visit:

programming video! !

The above is the detailed content of Detailed explanation of how to debug Golang projects in VSCode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cloud.tencent.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template