ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Golang Tools Integration

by liuhewei ST3

Golang tools (gofmt, gocode, golint, guru, gorename, ...) integration for Sublime Text 3

Details

Installs

  • Total 43K
  • Win 15K
  • Mac 16K
  • Linux 12K
Dec 21 Dec 20 Dec 19 Dec 18 Dec 17 Dec 16 Dec 15 Dec 14 Dec 13 Dec 12 Dec 11 Dec 10 Dec 9 Dec 8 Dec 7 Dec 6 Dec 5 Dec 4 Dec 3 Dec 2 Dec 1 Nov 30 Nov 29 Nov 28 Nov 27 Nov 26 Nov 25 Nov 24 Nov 23 Nov 22 Nov 21 Nov 20 Nov 19 Nov 18 Nov 17 Nov 16 Nov 15 Nov 14 Nov 13 Nov 12 Nov 11 Nov 10 Nov 9 Nov 8 Nov 7 Nov 6
Windows 0 0 1 0 2 0 0 0 1 1 0 2 1 0 2 0 0 2 1 3 0 1 0 1 0 0 0 0 1 3 2 0 2 1 1 2 2 0 0 1 0 2 0 1 0 1
Mac 1 0 2 0 2 0 1 0 2 1 0 0 0 0 2 0 0 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 3 0 1 1 0 0 0 0 1 1 0 0 0
Linux 0 0 0 2 3 1 0 0 0 0 2 0 1 0 0 0 0 1 3 2 1 0 0 1 0 0 0 2 6 0 0 0 1 0 2 0 1 0 0 2 2 0 0 0 3 0

Readme

Source
raw.​githubusercontent.​com

I've changed my IDE choice to JetBrains' Gogland.

Golang Tools Integration

It's a fork version of GoTools by ironcladlou. I create this project because ironcladlou has decided to abandon GoTools project, and I want to use this plugin together with Golang Build (by wbond) (discuss-1; discuss-2).

Features inherited from GoTools:

  • Auto-Completion: with gocode
  • Auto-Format: with gofmt or goimports
  • Go-to Definition: with guru
  • Rename: with gorename

Features removed(replaced) from GoTools:

  • Go syntax highlight: replaced by sublime's native support
  • Go build & test system: replaced by “Golang Build” (search in package-control)
  • GoTools Settings: replaced by “golangconfig
  • godef support: only use guru for “Go to definition”

Features added:

  • Auto-Lint: with golint or govet
  • Doc tips over selected text or cursor: with godoc

Usage

  • Step 1: Install golang and go tools by yourself: gocode, goimports, guru, gorename, golint, and make sure the $GOPATH/bin is added into $PATH (Or you can set them in Golang's global “Settings - User” later).

    go get -u -v github.com/nsf/gocode
    # OR mdempsky/gocode for better performance
    go get -u -v github.com/mdempsky/gocode
    
    go get -u -v github.com/golang/lint/golint
    go get -u -v golang.org/x/tools/cmd/guru
    go get -u -v golang.org/x/tools/cmd/goimports
    go get -u -v golang.org/x/tools/cmd/gorename
    
  • Step 2: Search and install “Golang Tools Integration” from package control.

  • Step 3(optional): Configure the Settings for golang and your project following the golang.sublime-settings and ExampleProject.sublime-project. Typically, the full features of 'guru' need use the configuration of the project.

Tips

  • If you want to trigger auto-completion after “.”, you can add below into Settings - Syntax specific - User (a.k.a. User/Go.sublime-settings):

    {
        "auto_complete_triggers": [{"selector": "source.go - string - comment - constant.numeric", "characters": "."}]
    }
    
  • If you want to ignore auto-completion when in comments, constant strings, and numbers, you can add below into Settings - Syntax specific - User (a.k.a. User/Go.sublime-settings):

    {
        "auto_complete_selector": "meta.tag - punctuation.definition.tag.begin, source - comment - string - constant.numeric"
    }
    

Introduction below comes from GoTools project

GoTools

GoTools is a Go programming language plugin for Sublime Text 3 inspired by vim-go. Rather than attempting to reinvent various supporting IDE components, it provides integration with existing community-supported tools.

Features

  • Jump to symbol/declaration using guru
  • Format and syntax check on save, including gutter marks (using gofmt)
  • Autocompletion (using gocode)
  • Build and test integration
  • Source analysis (using guru)
  • Identifier renaming (using gorename)
  • Improved syntax support (borrowed from GoSublime)

Prerequisites

GoTools will attempt to find all external Go tools (guru, gofmt, gocode, etc.) using GOPATH and GOROOT (not PATH). If you don't have these binaries, use go get to install them:

go get -u -v github.com/nsf/gocode
go get -u -v golang.org/x/tools/cmd/goimports
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v golang.org/x/tools/cmd/gorename

GoTools is only tested with Go 1.4. Note that gofmt is now included with the Go distribution, and any gofmt installed to GOPATH is likely from an old Go version and should probably be removed.

Installing

The easiest way to install GoTools is to use Package Control. Simply install Package Control, and then install the “GoTools” package using Package Control: Install Package from the command palette.

If you want to install GoTools manually, download the latest release and extract it to ~/.config/sublime-text-3/Packages/GoTools on Linux, or ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/GoTools on OSX.

Configuring GoTools

Create a GoTools settings file through the Sublime Text preferences menu at Package Settings -> GoTools -> Settings -> User.

Default settings are provided and can be accessed through the Sublime Text preferences menu at Package Settings -> GoTools -> Settings - Default. Each option is documented in the settings file itself.

Configuring Your Project

Create a GoTools settings key in a Sublime Text .sublime-project file (through the menu at Project -> Edit Project).

A documented example project file is provided.

Using GoTools

NOTE: Most GoTools commands are available via the Sublime Text command palette. Open the palette when viewing a Go source file and search for “GoTools” to see what's available.

Many of the build commands are also available via the context menu.

Format on Save

GoTools will format Go source buffers each time they're saved. To disable automatic formatting, set format_on_save in your GoTools settings.

Here's an example key binding which formats a source file when <ctrl>+<alt>+f is pressed:

{"keys": ["ctrl+alt+f"], "command": "gotools_format"}

By default gofmt is used for formatting. To change the backend, set format_backend in your GoTools settings. goimports is also available, as well as the option to first run goimports, then gofmt. This third option is useful when you want the automatic import resolution as well as the simplification (-s) feature from gofmt at the same time.

Go to Definition

GoTools provides a gotools_goto_def Sublime Text command which will jump to the symbol definition at the cursor.

Here's an example key binding which will go to a definition when <ctrl+g> is pressed:

{"keys": ["ctrl+g"], "command": "gotools_goto_def"}

Here's an example sublime-mousemap entry which will go to a definition using <ctrl>+<left mouse>:

{"button": "button1", "count": 1, "modifiers": ["ctrl"], "command": "gotools_goto_def"}

Autocomplete

GoTools integrates the Sublime Text autocompletion engine with gocode.

Here's an example key binding which autocompletes when <ctrl>+<space> is pressed:

{"keys": ["ctrl+space"], "command": "auto_complete"}

When suggestions are available, a specially formatted suggestion list will appear, including type information for each suggestion.

To disable autocompletion integration, set autocomplete in your GoTools settings.

Builds

GoTools integrates the Sublime Text build system with go build.

Activate the GoTools build system from the Sublime Text menu by selecting it from Tools -> Build System. If the build system is set to Automatic, GoTools will be automatically used for builds when editing Go source files.

There are several ways to perform a build:

  • From the Sublime Text menu at Tools -> Build
  • A key bound to the build command
  • The command palette, as Build: Build

A “Clean Build” command variant is also provided which recursively deletes all GOPATH/pkg directory contents prior to executing the build as usual.

Build results are placed in the Sublime Text build output panel which can be toggled with a command such as:

{ "keys" : ["ctrl+m"], "command" : "show_panel" , "args" : {"panel": "output.exec", "toggle": true}},

Here's an example key binding which runs a build when <ctrl>+b is pressed:

{ "keys": ["ctrl+b"], "command": "build" },

Here's an example key binding which runs “Clean Build” when <ctrl>+<alt>+b is pressed:

{ "keys": ["ctrl+alt+b"], "command": "build", "args": {"variant": "Clean Build"}},

Tests

GoTools integrates the Sublime Text build system with go test.

GoTools attempts to “do what you mean” depending on context. For instance, when using “Run Test at Cursor” in a test file which requires an integration Go build tag, GoTools will notice this and automatically add -tags integration to the test execution.

The following GoTools build variants are available:

Variant Description
Run Tests Discovers test packages based on the project_package and test_packages settings relative to the project gopath and executes them.
Run Test at Cursor Runs a single test method at or surrounding the cursor.
Run Current Package Tests Runs tests for the package containing the current file.
Run Tagged Tests Like “Run Tests” but for the packages specified in the tagged_packages setting.
Run Last Test Runs the last test variant that was executed.

Test results are placed in the built-in Sublime Text build output panel which can be toggled with a command such as:

{ "keys" : ["ctrl+m"], "command" : "show_panel" , "args" : {"panel": "output.exec", "toggle": true}},

Here's an example key binding which runs the test at the cursor when <ctrl>+<alt>+t is pressed:

{ "keys": ["ctrl+alt+t"], "command": "build", "args": {"variant": "Run Test at Cursor"}},

Replace variant in the command with any variant name from the preceding table for other bindings.

Oracle Analysis (experimental)

GoTools integrates Sublime Text with guru. Oracle is invoked with the gotools_guru Sublime Text command.

Here's an example which runs the guru “implements” command when <ctrl+alt+i> is pressed:

{ "keys" : ["ctrl+alt+i"], "command" : "gotools_guru" , "args" : {"command": "implements"}},

The following guru operations are supported as arguments to the gotools_guru command:

Command Notes
callers Slow on large codebases.
callees Slow on large codebases.
callstack Slow on large codebases.
describe
freevars Requires a selection.
implements
peers
referrers

Oracle results are placed in a Sublime Text output panel which can be toggled with a command such as:

{ "keys" : ["ctrl+m"], "command" : "show_panel" , "args" : {"panel": "output.gotools_guru", "toggle": true}},

Rename (experimental)

GoTools provides a gotools_rename command supported by gorename which supports type-safe renaming of identifiers.

When the gotools_rename command is executed, an input panel labeled Go rename: will appear. Rename results are placed in a Sublime Text output panel which can be toggled with a command such as:

{ "keys" : ["ctrl+m"], "command" : "show_panel" , "args" : {"panel": "output.gotools_rename", "toggle": true}},

Important: The gorename tool writes files in-place with no option for a dry-run. Changes might be destructive, and the tool is known to have bugs.

Gocode Caveats

Important: Using gocode support will modify the lib-path setting in the gocode daemon. The change will affect all clients, including other Sublime Text sessions, Vim instances, etc. Don't use this setting if you're concerned about interoperability with other tools which integrate with gocode.

Some projects make use of a dependency isolation tool such as Godep, and many projects use some sort of custom build script. Additionally, gocode uses a client/server architecture, and at present relies on a global server-side setting to resolve Go package paths for suggestion computation. By default, gocode will only search GOROOT and GOPATH/pkg for packages, which may be insufficient if the project compiles source to multiple GOPATH entries (such as Godeps/_workspace/pkg).

With such a project, to get the best suggestions from gocode, it's necessary to configure the gocode daemon prior to client suggestion requests to inform gocode about the locations of compiled packages for the project.

GoTools will infer the correct gocode lib-path by constructing a path which incorporates all project GOPATH entries.

GoSublime Caveats

Installing GoTools alongside GoSublime isn't tested or supported, so YMMV.