If you are a Sublime Text user, and looking to set up gopls integration with it, you have arrived at the right place. The primary documentation for gopls assumes you are using VSCode; and the rest are using GoLand, which leaves us Sublime Text users in a tight spot. This post attempts to fill that gap.
The official documentation here just mentions how to install gopls, which is barely enough. But for the sake of completeness, I will go through the entire set of steps.
Installation
- Install
gopls
on your machine.- Go to any temp directory and run
go get golang.org/x/tools/gopls@latest
. - If you see the error
go: cannot use path@version syntax in GOPATH mode
, then runGO111MODULE=on go get golang.org/x/tools/gopls@latest
- Check that the
gopls
binary got installed by runningwhich gopls
.
- Go to any temp directory and run
- Open the Command Pallete (Shift+Ctrl+p). Select “Install Package”
- Select “LSP”.
- Open the Command Pallete again.
- Select “LSP: Enable Language Server Globally”.
- Select “gopls”.
This completes the installation part, which is half the battle. Next up, we need to configure gopls
.
Configuration
-
Navigate to Preferences > Package Settings > LSP > Settings. In the User settings section, paste this:
{ "clients": { "gopls": { "command": ["/home/agniva/go/bin/gopls"], "enabled": true, "env": { "PATH": "/home/agniva/go/bin:/usr/local/go/bin" }, "scopes":["source.go"], "syntaxes": [ "Packages/Go/Go.sublime-syntax", ], "settings": { "gopls.usePlaceholders": true, "gopls.completeUnimported": true, }, "languageId": "go" } }, "only_show_lsp_completions": true, "show_references_in_quick_panel": true, "log_debug": true, "log_stderr": true }
Adjust the file paths accordingly.
-
There are several things to note here. Depending on your shell settings, you may need to pass the full file path. Otherwise, you might see the error “Could not start gopls. I/O timeout.”
-
Any custom settings need to be placed under the
settings
key. And the key names need to be prefixed with “gopls.”. For the full list of settings, check here. -
Navigate to Preferences > Package Settings > LSP > Key Bindings. You will see that a lot of commands have keymap as “UNBOUND”. Set them as per your old shortcuts.
-
Open the Command Pallete. Select “LSP: Restart Servers”.
-
Enjoy a working setup of gopls.
Hopefully this was helpful. As always, please feel free to suggest any improvements in the comments.