When it comes to Integrated Development Environments (IDE), there are so many choices it’s easy to get lost. In my case, I’m always looking for simplicity, command line interface and open source, so with these criteria in mind I ended up choosing Neovim as my main editor with NvChad to help me configure a full-blown IDE on top of it. In particular, in this blog post I’ll show you how to set it up for Golang development on Arch Linux.

Installation of Neovim

Let start by installing the latest version of Neovim which is 0.10.2-2 at the time of this writing, version 0.10 or later is required for NvChad to work properly.

pacman -S neovim

On MacOS you can run

brew install neovim

Dependencies

First, make sure you have all the necessary dependencies installed on our system:

  • Nerd fonts - a collection of modified fonts for developers
  • gopls - the most used language server for Go.
  • Ripgrep (optional for grep searching with Telescope)
  • gcc
  • make
sudo pacman -S $(pacman -Sgq nerd-fonts)
sudo pacman -S gopls ripgrep gcc make

Installation of NvChad

NvChad is a modern, customisable and fast **Neovim configuration ** built using Lua. It’s quite useful for being more productive when using Neovim, so let’s install it with this simple command

git clone https://github.com/NvChad/starter ~/.config/nvim && nvim

This command simply clones the NvChad config to ~/.config/nvim' and runs nvvim’. When the editor starts, it will automatically install the following plugins

  • base64 - for theme management
  • NvChad - basic configuration that allow the user configuration to be entered into NeoVim
  • nvim-treesitter - for analysis and highlighting of code
  • ui - the editor interface (statusline, tabufline..)

And many more by leveraging LazyVim package manager.

If you’re curious You can access the documentation to learn about NvChad’s base46 and UI

:h nvui

For example you can now bring the theme switcher with [space] t h I’m using gruvbox

Now you need to configure Neovim to use gopls by editing

vi ~/.config/nvim/lua/configs/lspconfig.lua
lspconfig.gopls.setup({
    cmd = { "gopls" },
    filetypes = { "go", "gomod", "gowork", "gotmpl" },
    root_dir = lspconfig.util.root_pattern("go.work", "go.mod", ".git"),
    settings = {
        gopls = {
            analyses = {
                unusedparams = true,
            },
            staticcheck = true,
		completeUnimported = true,
	    usePlaceholders = true,
        },
    },
})
  • completeUnimported auto import what’s required by your code
  • unusedparams alert if you declare something but have don’t use it.

For the best development experience, ensure you have tools like goimports or gofumpt installed:

go install golang.org/x/tools/cmd/goimports@latest
go install mvdan.cc/gofumpt@latest

You can add custom keymappings

vi mappings.lua
map("n", "<leader>fm", ":lua vim.lsp.buf.format()<CR>", { desc = "Format Golang code" })
map("n", "gd",  "<cmd>lua vim.lsp.buf.definition()<CR>", { desc = "Go to Definition"})
map("n","gr", "<cmd>lua vim.lsp.buf.references()<CR>", { desc = "Find References"})
map("n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", { desc = "Hover Documentation"})
map("n", "<leader>ca", "<cmd>lua vim.lsp.buf.code_action()<CR>", {desc = "Code Action" })
map("n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", {desc ="Rename Symbol"})

Mason.nvim

Most of the info online is now obsolete regarding adding key mappings you now have to follow the vim format as described above. For Neovim to become an IDE we need to interact with a Language Server (LSP) to format, lint and debug code. So let us set up a tool called Mason.nvim which will be used to manage LSPs and other development tools. Install it from Neovim with this command

:MasonInstallAll

It will install the following additional packages

  • css-lsp
  • html-lsp
  • lua-language-server
  • stylua But Mason.nvim provides many more options.

gopls

You should now be able to open some go code and check that you indeed have LSP available

nvim main.go
:LspInfo

You should see gopls listed as the active server, if that’s the case you’re ready to code.

Cheatsheet

General

  • CTRL n bring file explorer
  • :e tab or <path> open a file, navigate list with tab
  • :split - Open the file in an horizontal split screen.
  • :vsplit - Open the file in a vertical split screen.
  • :tabedit - Open the file in a new tab.

Golang space

  • fm format Golang code
  • gd go to Definition
  • gr find References
  • K hover Documentation
  • ca code action
  • rn rename symbol

Telescope space

  • ff find files
  • fh help files
  • :Telescope fd access Telescope’s find file feature

Moving around ctrl

  • b to go to the beginning of the line
  • e` to go to the end of the lineL
  • ] enter a tag

LazyVim

  • :Lazy access Lazy package manager
  • :Lazy update [plugins] update plugins
  • <CR> on a plugin to see its details
  • K open plugin link

Customize Hotkeys

Keep in mind the following while setting up new hotkeys in ~/.config/nvim/lua/configs/mappings.lua the new syntax is

map("<n|i|v|c>","<key combinaison>", "<cmd><CMD><CR>", { desc = "<DESCRIPTION>"})
  • mode can be: normal, insert, visual or command line
  • key combinaison can contain:
    • leader = Space
    • A = Alt
    • C = Ctrl
    • S = Shift
  • <cmd><CR> command to execute
  • desc document the mapping, shown on helpful on-screen display

Troubleshooting

  • make sure gopls is in your path
  • If you can’t get LSP to work you can watch the log with `:LspLog
  • if you can the error No package found for open file make sure you’ve opened the file from the root directory of your project, you can check that with :pwd
    • if it’s a module, ensure your project is inside a Go module run go mod init <module name>
    • if the file already exist check its validity go mod tidy