Debugging Golang Engineering with visual Studio code

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Key words

    • The simplest debugging strategy
    • Multi-project commissioning for personal development and project development
    • No need to modify system environment variables

Prepare Vscode

Download the latest version of Vscode on the website:

Visual Studio Code-code Editing. redefined**

Installing the Golang Plugin

    • Open the expansion panel

      vscode-> View, extension

    • Find the Go plugin in the search box, enter go, find the second line that has Rich Go language support for Visual Studio code plug-ins, click Install

      Note not the highest ranked

    • Restart Editor

Configure Startup Items

    • Open the Debug panel

      Vscode-> View-Debug

    • Add a debug target

      In the "No Debug" drop-down box, click "Add Configuration ..."

    • To add a 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        }    ]}

Where: "Port", "host" are automatically generated by the Go plugin

"ENV" for setting environment variables, set as your project directory can (contains bin, src folder)

Preparing to debug plugins

At this time find Main.go press F5, will error tip:

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 the Gopath (project directory)

Start debugging

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

Debugging shortcut keys is consistent with Visual Studio system

    • F9 Toggle Breakpoint
    • F10 Step Over
    • F11 Step in
    • Shift+f11 Step out

Watch out.

    • When some struct members cannot be displayed directly, the variable name can be directly selected, added to the watch, or right-clicked: "Debug: Evaluate"

Multi-Project Commissioning

Multiple sets of debug portals can be added to the Launch.json, and the corresponding configuration is selected in the debug panel to enable debugging of 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        }    ]}

"${filedirname}" in "program" is the starting point with the currently selected file

It is also recommended to use "program", "${workspaceroot}", as the package name as the starting point of the way to configure

Reference links

Debugging in Visual Studio Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.