Setting Up gopls in Neovim Using nvim-lspconfig and lazy.nvim

Setting Up gopls in Neovim Using nvim-lspconfig and lazy.nvim
4 min read

Neovim, with its modern architecture and extensive plugin ecosystem, provides an excellent environment for development in various programming languages. For Go developers, integrating gopls (the Go language server) with Neovim can significantly enhance the development experience. This article will guide you through setting up gopls in Neovim using nvim-lspconfig and lazy.nvim.

Prerequisites

Before we begin, ensure you have the following installed:

  1. Go Language: You can download and install Go from the official Go website.
  2. Neovim: Download and install Neovim from the Neovim website.
  3. gopls: Install the Go language server by running the following command:
    go install golang.org/x/tools/gopls@latest
    

Step 1: Configure lazy.nvim

First, ensure that lazy.nvim is installed and properly configured in your Neovim setup. If you haven’t installed lazy.nvim yet, you can do so by following the instructions on its GitHub repository.

Install nvim-lspconfig

In your lazy.nvim setup file (usually init.lua or a similar configuration file), add the nvim-lspconfig plugin:

return require('lazy').setup({
    -- Other plugins
    { 'neovim/nvim-lspconfig' },
})

Step 2: Configure gopls with nvim-lspconfig

Next, we need to configure gopls using nvim-lspconfig. Add the following configuration to your Neovim setup:

local lspconfig = require('lspconfig')

lspconfig.gopls.setup({
  cmd = { "gopls" },
  filetypes = { "go", "gomod" },
  root_dir = lspconfig.util.root_pattern("go.work", "go.mod", ".git"),
  settings = {
    gopls = {
      analyses = {
        unusedparams = true,
      },
      staticcheck = true,
    },
  },
})

This configuration tells Neovim to use gopls for Go files and sets some useful gopls settings, such as enabling the unusedparams analysis and staticcheck.

Step 3: Additional LSP Configuration (Optional)

For an enhanced development experience, you may want to include additional LSP configurations, such as keybindings and completion settings. Here is an example configuration:

local nvim_lsp = require('lspconfig')
local on_attach = function(client, bufnr)
  local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
  local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end

  buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')

  local opts = { noremap=true, silent=true }

  buf_set_keymap('n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts)
  buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
  buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
  buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
  buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
  buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
  buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
  buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
  buf_set_keymap('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
  buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
  buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
  buf_set_keymap('n', '<space>e', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts)
  buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
  buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
  buf_set_keymap('n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts)
  buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
end

nvim_lsp.gopls.setup({
  on_attach = on_attach,
  cmd = { "gopls" },
  filetypes = { "go", "gomod" },
  root_dir = nvim_lsp.util.root_pattern("go.work", "go.mod", ".git"),
  settings = {
    gopls = {
      analyses = {
        unusedparams = true,
      },
      staticcheck = true,
    },
  },
})

Explanation

  • on_attach: This function is called when the language server is attached to a buffer. It sets up keybindings for various LSP functions.
  • cmd: Specifies the command to start the gopls server.
  • filetypes: Lists the file types that gopls should handle.
  • root_dir: Determines the project root directory.
  • settings: Configures specific gopls settings, such as enabling static analysis checks.

Conclusion

By following these steps, you should have a fully functional Go development environment in Neovim, leveraging gopls for enhanced code intelligence and navigation. Adjust the configuration to fit your specific needs and workflow. Happy coding!

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Den W. 2K
I'm a passionate tech enthusiast who loves diving into the world of software, programming, and tech reviews.
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In