Skip to content
Rebecca Stambler edited this page Aug 13, 2019 · 118 revisions

gopls (pronounced: "go please") is an implementation of the Language Server Protocol (LSP) server for Go. LSP allows any text editor to be extended with IDE-like features (see https://langserver.org for details).

It is currently in alpha, so it is not stable.

To install or update gopls, run go get golang.org/x/tools/gopls@latest.

Do not use the -u flag, as it will update your dependencies to incompatible versions.

If you see this error:

$ go get golang.org/x/tools/gopls@latest
go: cannot use path@version syntax in GOPATH mode

run GO111MODULE=on go get golang.org/x/tools/gopls@latest.

Table of Contents

Troubleshooting
Known Issues
Installation
Status
Contributing
FAQ
Additional Information

Troubleshooting

If you see a gopls error or crash, or gopls just stops working, please file an issue on the Go issue tracker. Along with an explanation of the issue, please share the information listed here:

  • Your editor and any settings you have configured (for example, your VSCode settings.json file).
  • A sample program that reproduces the issue, if possible.
  • The output of gopls version on the command line.
  • The output of gopls -rpc.trace -v check /path/to/file.go.
  • gopls logs from when the issue occurred, as well as a timestamp for when the issue began to occur. See the instructions below for information on how to capture gopls logs.

Capturing gopls logs

For VSCode users, the gopls log can be found by going to "View: Debug Console" -> "Output" -> "Tasks" -> "gopls". For other editors, you may have to directly pass a -logfile flag to gopls.

To increase the level of detail in your logs, start gopls with the -rpc.trace flag. To start a debug server that will allow you to see profiles and memory usage, start gopls with serve --debug=localhost:6060. See the editor configurations section below for information on how to pass these flags via your editor.

Restart your editor

Once you have filed an issue, you can then try to restart your gopls instance by restarting your editor. In many cases, this will correct the problem. In VSCode, the easiest way to restart the language server is by opening the command palette (Ctrl + Shift + P) and selecting "Go: Restart Language Server". You can also reload the VSCode instance by selecting "Developer: Reload Window".

Feel free to ask questions about gopls on the #gopls Gopher Slack channel.

Known Issues

  • Cursor resets to the beginning or end of file on format: #31937.
  • Editing multiple modules in one editor window: #32394.
  • Language features do not work with cgo: #32898.
  • Does not work with build tags: #29202.
  • Find references and rename only work in a single package: #32869, #32877.
  • Completion does not work well after go or defer statements: #29313.

Installation

To install or update gopls, run go get golang.org/x/tools/gopls@latest.

Do not use the -u flag, as it will update your dependencies to incompatible versions.

If you see this error:

$ go get golang.org/x/tools/gopls@latest
go: cannot use path@version syntax in GOPATH mode

run GO111MODULE=on go get golang.org/x/tools/gopls@latest.

Integration with your text editor

VSCode

Use the VSCode-Go plugin, with the following configuration:

"go.useLanguageServer": true,
"[go]": {
    "editor.snippetSuggestions": "none",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
},
"gopls": {
    "usePlaceholders": true, // add parameter placeholders when completing a function
    "wantCompletionDocumentation": true // for documentation in completion items
},
"files.eol": "\n", // formatting only supports LF line endings

VSCode will complain about the "gopls" settings, but they will still work. Once we have a consistent set of settings, we will make the changes in the VSCode plugin necessary to remove the errors.

If you encounter problems with import organization, please try setting a higher code action timeout (any value greater than 750ms), for example:

"[go]": {
  "editor.codeActionsOnSaveTimeout": 3000
}

To enable more detailed debug information, add the following to your VSCode settings:

"go.languageServerFlags": [
    "-rpc.trace", // for more detailed debug logging
    "serve",
    "--debug=localhost:6060", // to investigate memory usage, see profiles 
],

You can disable features through the "go.languageServerExperimentalFeatures" section of the config. An example of a feature you may want to disable is "documentLink", which opens Godoc links when you click on import statements in your file.


Vim / Neovim

Use vim-go ver 1.20+, with the following configuration:

let g:go_def_mode='gopls'
let g:go_info_mode='gopls'

or

Use LanguageClient-neovim, with the following configuration:

" Launch gopls when Go files are in use
let g:LanguageClient_serverCommands = {
       \ 'go': ['gopls']
       \ }
" Run gofmt on save
autocmd BufWritePre *.go :call LanguageClient#textDocument_formatting_sync()

or

Use ale:

let g:ale_linters = {
	\ 'go': ['gopls'],
	\}

see this issue

or

Use prabirshrestha/vim-lsp, with the following configuration:

augroup LspGo
  au!
  autocmd User lsp_setup call lsp#register_server({
      \ 'name': 'go-lang',
      \ 'cmd': {server_info->['gopls']},
      \ 'whitelist': ['go'],
      \ })
  autocmd FileType go setlocal omnifunc=lsp#complete
  "autocmd FileType go nmap <buffer> gd <plug>(lsp-definition)
  "autocmd FileType go nmap <buffer> ,n <plug>(lsp-next-error)
  "autocmd FileType go nmap <buffer> ,p <plug>(lsp-previous-error)
augroup END

or

Use natebosch/vim-lsc, with the following configuration:

let g:lsc_server_commands = {
\  "go": {
\    "command": "gopls serve",
\    "log_level": -1,
\  },
\}

The log_level part is needed to prevent breakage from logging. See natebosch/vim-lsc#180.

or

Use coc.nvim, with the following coc-settings.json configuration:

  "languageserver": {
    "golang": {
      "command": "gopls",
      "rootPatterns": ["go.mod", ".vim/", ".git/", ".hg/"],
      "filetypes": ["go"]
    }
  }

The editor.action.organizeImport code action will auto-format code and add missing imports. To run this automatically on save, add the following line to your init.vim:

autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport')

Vim (classic only)

Use the experimental govim, simply follow the install steps.


Emacs

Use lsp-mode. gopls is built in now as a client, so no special config is necessary. You first must install gopls and put it somewhere in your PATH. Here is an example (assuming you are using use-package) to get you started:

(use-package lsp-mode
  :commands (lsp lsp-deferred))

(add-hook 'go-mode-hook #'lsp-deferred)

;; optional - provides fancier overlays
(use-package lsp-ui
  :commands lsp-ui-mode)

;; if you use company-mode for completion (otherwise, complete-at-point works out of the box):
(use-package company-lsp
  :commands company-lsp)

Common errors:

  • When prompted by Emacs for your project folder, if you are using modules you must select the module's root folder (i.e. the directory with the "go.mod"). If you are using GOPATH, select your $GOPATH as your folder.
  • Emacs must have your environment set properly (PATH, GOPATH, etc). You can run M-x getenv <RET> PATH <RET> to see if your PATH is set in Emacs. If not, you can try starting Emacs from your terminal, using this package, or moving your shell config from .bashrc into .bashenv (or .zshenv).
  • Make sure lsp-mode, lsp-ui and company-lsp are up-to-date, and make sure lsp-go is not installed.

To troubleshoot, look in the *lsp-log* buffer for errors.


Acme

Use the experimental acme-lsp, simply follow the install steps.


Sublime Text

Use the LSP package. After installing it using Package Control, do the following:

  • Open the Command Palette
  • Find and run the command LSP: Enable Language Server Globally
  • Select the gopls item. Be careful not to select the similarly named golsp by mistake.

Finally, you should familiarise yourself with the LSP package's Settings and Key Bindings. Find them under the menu item Preferences > Package Settings > LSP.


Status

gopls is currently under active development by the Go team. The code is in the x/tools repository, in golang.org/x/tools/internal/lsp and golang.org/x/tools/gopls.

Supported Features

  • Autocompletion
  • Jump to definition
  • Signature help
  • Hover
  • Document symbols
  • References
  • Rename

Contributing

See https://github.com/golang/go/wiki/Contributing-to-gopls.

Contributions are welcome, but since development is so active, we request that you file an issue and claim it before starting to work on something. Otherwise, it is likely that we might already be working on a fix for your issue.

All gopls issues are labeled as such (see the gopls label). Available issues are also tagged help wanted (see them here). Before you begin working on an issue, please leave a comment that you are claiming it.

FAQ

  • Why is it called gopls? Since gopls works both as a language server and as a command line tool, we wanted a name that could be used as a verb. For example, gopls check should read as "go please check." See: golang.org/cl/158197.

Additional Information

Clone this wiki locally