Skip to content
Artyom Andreev edited this page Jan 28, 2023 · 30 revisions

Installing server

Since January 2021 gopls is the official language server for Go.

To install it:

go install golang.org/x/tools/gopls@latest

Registering the language server in .vimrc

if executable('gopls')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'gopls',
        \ 'cmd': {server_info->['gopls', '-remote=auto']},
        \ 'allowlist': ['go', 'gomod', 'gohtmltmpl', 'gotexttmpl'],
        \ })
    autocmd BufWritePre *.go
        \ call execute('LspDocumentFormatSync') |
        \ call execute('LspCodeActionSync source.organizeImports')
endif

The purpose of the -remote=auto option is to prevent launching the same server multiple times if it was already started by some other plugin, which will often be the case, since vim-go is widely used.

If you are also using vim-go, you need to disable the completion provided by it, which is enabled by default. Add this to your ~/.vimrc:

let g:go_code_completion_enabled = 1

Alternative registration method

If you are a Plug user you may also register the gopls LSP server using:

Plug 'prabirshrestha/vim-lsp'
...
Plug 'piec/vim-lsp-gopls'