5 Use [vim-go] ver 1.20+, with the following configuration:
8 let g:go_def_mode='gopls'
9 let g:go_info_mode='gopls'
12 ## LanguageClient-neovim
14 Use [LanguageClient-neovim], with the following configuration:
17 " Launch gopls when Go files are in use
18 let g:LanguageClient_serverCommands = {
22 autocmd BufWritePre *.go :call LanguageClient#textDocument_formatting_sync()
35 see [this issue][ale-issue-2179]
39 Use [prabirshrestha/vim-lsp], with the following configuration:
44 autocmd User lsp_setup call lsp#register_server({
46 \ 'cmd': {server_info->['gopls']},
47 \ 'whitelist': ['go'],
49 autocmd FileType go setlocal omnifunc=lsp#complete
50 "autocmd FileType go nmap <buffer> gd <plug>(lsp-definition)
51 "autocmd FileType go nmap <buffer> ,n <plug>(lsp-next-error)
52 "autocmd FileType go nmap <buffer> ,p <plug>(lsp-previous-error)
58 Use [natebosch/vim-lsc], with the following configuration:
61 let g:lsc_server_commands = {
63 \ "command": "gopls serve",
65 \ "suppress_stderr": v:true,
70 The `log_level` and `suppress_stderr` parts are needed to prevent breakage from logging. See
71 issues [#180](https://github.com/natebosch/vim-lsc/issues/180) and
72 [#213](https://github.com/natebosch/vim-lsc/issues/213).
76 Use [coc.nvim], with the following `coc-settings.json` configuration:
82 "rootPatterns": ["go.mod", ".vim/", ".git/", ".hg/"],
84 "initializationOptions": {
85 "usePlaceholders": true
91 Other [settings](settings.md) can be added in `initializationOptions` too.
93 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`:
96 autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport')
101 In vim classic only, use the experimental [`govim`], simply follow the [install steps][govim-install].
105 To use the new (still experimental) native LSP client in Neovim, make sure you
106 [install][nvim-install] the prerelease v0.5.0 version of Neovim (aka “nightly”),
107 the `nvim-lspconfig` configuration helper plugin, and check the
108 [`gopls` configuration section][nvim-lspconfig] there.
110 ### Custom configuration
112 You can add custom configuration using Lua. Here is an example of enabling the
113 `unusedparams` check as well as `staticcheck`:
117 nvim_lsp = require "nvim_lsp"
118 nvim_lsp.gopls.setup {
119 cmd = {"gopls", "serve"},
134 To get your imports ordered on save, like `goimports` does, you can define
135 a helper function in Lua:
141 function goimports(timeoutms)
142 local context = { source = { organizeImports = true } }
143 vim.validate { context = { context, "t", true } }
145 local params = vim.lsp.util.make_range_params()
146 params.context = context
148 local method = "textDocument/codeAction"
149 local resp = vim.lsp.buf_request_sync(0, method, params, timeoutms)
150 if resp and resp[1] then
151 local result = resp[1].result
152 if result and result[1] then
153 local edit = result[1].edit
154 vim.lsp.util.apply_workspace_edit(edit)
158 vim.lsp.buf.formatting()
162 autocmd BufWritePre *.go lua goimports(1000)
165 (Taken from the [discussion][nvim-lspconfig-imports] on Neovim issue tracker.)
169 To make your <kbd>Ctrl</kbd>+<kbd>x</kbd>,<kbd>Ctrl</kbd>+<kbd>o</kbd> work, add
170 this to your `init.vim`:
173 autocmd FileType go setlocal omnifunc=v:lua.vim.lsp.omnifunc
178 * [Neovim's official LSP documentation][nvim-docs].
180 [vim-go]: https://github.com/fatih/vim-go
181 [LanguageClient-neovim]: https://github.com/autozimu/LanguageClient-neovim
182 [ale]: https://github.com/w0rp/ale
183 [ale-issue-2179]: https://github.com/w0rp/ale/issues/2179
184 [prabirshrestha/vim-lsp]: https://github.com/prabirshrestha/vim-lsp/
185 [natebosch/vim-lsc]: https://github.com/natebosch/vim-lsc/
186 [natebosch/vim-lsc#180]: https://github.com/natebosch/vim-lsc/issues/180
187 [coc.nvim]: https://github.com/neoclide/coc.nvim/
188 [`govim`]: https://github.com/myitcv/govim
189 [govim-install]: https://github.com/myitcv/govim/blob/master/README.md#govim---go-development-plugin-for-vim8
190 [nvim-docs]: https://neovim.io/doc/user/lsp.html
191 [nvim-install]: https://github.com/neovim/neovim/wiki/Installing-Neovim
192 [nvim-lspconfig]: https://github.com/neovim/nvim-lspconfig#gopls
193 [nvim-lspconfig-imports]: https://github.com/neovim/nvim-lspconfig/issues/115