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.
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
.
If you then see this error:
GO111MODULE=on go get golang.org/x/tools/gopls@latest go: cannot determine module path for source directory /home/user (outside GOPATH, no import comments)
This may be worth trying: go get golang.org/x/tools/gopls
Troubleshooting
Known Issues
Installation
Status
Contributing
FAQ
Additional Information
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:
settings.json
file).gopls version
on the command line.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.gopls
logsFor 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.
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.
cgo
: #32898.go
or defer
statements: #29313.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
.
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 }, "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.
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')
Use the experimental govim
, simply follow the install steps.
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:
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).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.
Use the experimental acme-lsp
, simply follow the install steps.
Use the LSP package. After installing it using Package Control, do the following:
Finally, you should familiarise yourself with the LSP package's Settings and Key Bindings. Find them under the menu item Preferences > Package Settings > LSP.
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.
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.
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.gopls
within an editor, see https://github.com/golang/go/wiki/gopls-integrator-FAQ.gopls
, see https://github.com/golang/go/wiki/Contributing-to-gopls.