Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Developing Go with VSCode

Developing Go with VSCode

Visual Studio Code (abbreviated as VS Code) is a free, open-source code editor developed by Microsoft that supports multiple programming languages and provides features such as syntax highlighting, intelligent code completion, code refactoring, and debugging.

With the official Go extension for Visual Studio Code, you get intelligent code completion, go-to-definition, symbol search, unit testing, program debugging, and many other practical features that help with Go project development.

If you are not yet familiar with VS Code or have not installed it, you can refer to:VSCode Tutorial。

Installing the Go Plugin

Open the VS Code Extensions marketplace and search for:Go Extension for VS Code。

In the editor, click Extensions on the left, then search for Go, and install the official plugin:

Once installed, we can start writing Go code. Type fmt. to try the intelligent code completion:


IntelliSense

IntelliSense is provided by the gopls language service maintained by the official Go team, and its behavior can be customized through gopls configuration options.

1. Semantic Highlighting

VS Code highlights code based on TextMate grammar by default. For better semantic-level highlighting, enable it in the settingsui.semanticTokens:

"gopls": { "ui.semanticTokens": true }

2. Code Auto-completion

When editing Go source code, the plugin pops up code completion candidates in real time, supporting member completion for the current package, imported packages, and packages not yet imported; typing a package name followed by . brings up method and constant hints for that package.

Shortcut keys:Ctrl+空格(⌃Space) manually trigger the completion popup

3. Hover Documentation

Hover the mouse over a variable, function, or struct, and a popup shows documentation comments, function signatures, and other details.

4. Function Parameter Hints

Type the opening parenthesis of a function(and the parameter signature pops up automatically; as you enter arguments, an underline follows to indicate the argument currently being filled in;

Shortcut keys:Shift+Cmd+空格(⇧⌘Space) with the cursor inside the parentheses, manually trigger the parameter hints

Code Navigation

The editor's right-click menu includes a full set of built-in code navigation features:

Open command paletteShift+Cmd+P(⇧⌘P), quickly jump via symbol search:

Using the Directive Go: Toggle Test File, quickly switch back and forth between business code and the corresponding test file.


Code Formatting

Formatting Configuration

  1. Disable format on save

    "[go]": {
    
     "editor.formatOnSave": false
    
    

} 2.

Set the Go plugin as the default formatter for Go files

"[go]": {

"editor.defaultFormatter": "golang.go"

}
3.

Enable the strict gofumpt formatting style (stricter than native gofmt)

"gopls": {

"formatting.gofumpt": true

}

Formatting relies on gopls under the hood


Unit testing

The VS Code sidebar testing panel + CodeLens quick buttons above the code supportunit testing, benchmark testing, and performance profiling offunctions / files / packages / the entire project.
Enter in the command palette Go: test to see all test commands; commonly used ones:

The first three commands can automatically generate test case skeletons based on gotests; the plugin supports configuration:


Debugging

The Go plugin relies on the Delve debugger to debug Go code, with unique capabilities:

  1. Local debugging + remote service debugging
  2. Use Delve expression syntax to view custom data in the debug panel
  3. Run native dlv commands in the debug console to dynamically modify debug settings and inspect variables
  4. ConfigurehideSystemGoroutinesHide/show system goroutines
  5. Right-click code to open the Disassembly View
  6. Experimental features: function call debugging, core file analysis, Mozilla rr reverse debugging

More Related Tools