Go Extension Pack
This extension pack include some of the popular (and some of my favorite) Go extensions. If you like it, please leave your Rating & Review and share with your friends. If you know any extension that is good for Go development, just let me know by creating an issue.
Extensions Included
Recommended Settings
Keyboard shortcuts
- Editing
Alt-o : Go: Show All Commands... (go.show.commands )
- Testing
Alt-j Alt-j : Go: Toggle Test File (go.toggle.test.file )
Alt-j Alt-c : Go: Toggle Test Coverage In Current Package (go.test.coverage )
- Code Generation
Alt-g Alt-g : Go: Generate Unit Tests For Function (go.test.generate.function )
Alt-g Alt-f : Go: Generate Unit Tests For File (go.test.generate.file )
Alt-g Alt-p : Go: Generate Unit Tests For Package (go.test.generate.package )
Alt-g Alt-i : Go: Generate Interface Stubs (go.test.generate.package )
Alt-g Alt-t : Go: Add Tags To Struct FIelds (go.add.tags )
Alt-g Alt-r : Go: Add Tags From Struct FIelds (go.remove.tags )
Visual Studio Code
Go
In order to use Rename Symbol (gorename) feature, you have to enable Go: Use Language Server option. See this and this.
gopls (pronounced: "go please") is the official language server for the Go language. Check the Go for Visual Studio Code for further information. For complete gopls Settings, please check here.
{
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
},
},
"[go.mod]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
},
}
}
Code Runner
code-runner.saveAllFilesBeforeRun : You better save .go files before run. So let's save it automatically.
code-runner.ignoreSelection : It's almost impossible to run selected code in Go. So let's ignore it.
code-runner.runInTerminal : Code Runner run code in Output pane by default. I prefer run in Terminal.
{
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.ignoreSelection": true,
"code-runner.runInTerminal": true
}
There is also an important settings for Go run. By default, Code Runner run script for currently opened Go file. But mostly we run entire main package in current directory. We need to configure code-runner.executorMap setting to achieve that by ignoring the file in the command line. The following setting are very platform-specific, so choose your platform and configure it.
Windows Command Prompt
"code-runner.executorMap": {
"go": "go run . & REM"
}
Linux/macOS/Shell/PowerShell
"code-runner.executorMap": {
"go": "go run . #"
}
Editor: Snippet Suggestions
Show snippet suggestions on top of others to improve coding experiences.
{
"editor.snippetSuggestions": "top"
}
Configure Tasks
There are two common/built-in task called Tasks: Run Build Task (Ctrl-Shift-B ) and Tasks: Run Test Task (Ctrl-Shift-/ ) in the VSCode. So I usually use these for common Build/Test tasks for Go projects.
By default, the Tasks: Run Test Task don't have a keyboard shortcut. You must binding this by yourself.
Just add a .vscode/tasks.json file into your workspace with the following content.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "go",
"type": "shell",
"args": [
"build",
"-o",
"dist/${workspaceFolderBasename}"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
},
{
"label": "test",
"command": "go",
"type": "shell",
"args": [
"test",
],
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
More info: https://go.microsoft.com/fwlink/?LinkId=733558
Snippets Included
This extension will contains supplementary code snippets to Go extension. Stay tuned.
Go snippets
Go Samples
g-cli : Generate Go CLI sample with flag package
g-gin-crud : Generate gin CRUD sample
g-gorm-crud : Generate GORM CRUD sample
statements
g-forc : Snippet for a for loop with custom condition
g-forb : Snippet for a for loop with a break (infinite loop)
g-forr : Snippet for a for range loop with better hints
g-iferr : Snippet for if err != nil with common usage scenario
g-enum or g-const-iota : Generate enum-like const
g-trycatch or g-recover : Generate try/catch-like statements
fmt
fv : fmt.Printf() with variable content
fvv : fmt.Printf() with variable type and content
builtin
g-make-chan-buffered : make buffered channel
g-make-chan-unbuffered : make unbuffered channel
g-make-map : make map
g-make-slice : make slice
log
g-log-new : log.New() with various options
g-log-file : log.SetOutput to a file
g-log-file-console : log.SetOutput to console & file
math/rand
g-rand : Generate Random Number snippet
net/http
g-http-get : Generate http.Get snippet
g-http-post : Generate http.Post snippet
g-http-put : Generate http.Put snippet
g-http-delete : Generate http.Delete snippet
net/json
g-json-marshal : Generate json.Marshal
g-json-unmarshal : Generate json.Unmarshal
g-json-newdecoder : Generate json.NewDecoder
ioutil
g-ioutil-writefile : Generate ioutil.WriteFile
g-ioutil-readfile : Generate ioutil.ReadFile
gin
g-gin-func : Generate gin func snippet
g-gin-action : Generate gin action snippet
g-gin-controller : Generate gin controller snippet
g-gin-binding-tags : Generate gin binding tags
swagger
g-swag-main : Generate swag main snippet
GORM
Testing
g-test : Generate Test_* func template
g-benchmark : Generate Benchmark* func template
TODO
- Add "Comments" to each declaration snippets (
go vet )
Dockerfile snippets
g-dockerfile : Multi-stage Dockerfile for Go
Enjoy!
| |