Go build tags + Emacs + gopls

With eglot being integratd in Emacs and gopls being the language server of choice I’ve been following this blog to configure my emacs for go, combined with readme instructions of gopls and eglot itself. Since at work we are using a monorepo setup for all go related code, we are also using build tags to customize the build, to make this work with gopls additional information needs to be passed to the language-server depending on the workspace. This is done differently depending on the editor interacting with gopls, based on the neovim example this can be replicated in emacs, and conviniently using .dir-locals.el to automatically configure the project correctly when opened.

((nil
  . ((eglot-workspace-configuration
      . (:gopls (:buildFlags  ["-tags=SOMETAG ANOTHERTAG"])))))))

It is important to use [ ... ] here which represents a vector in elisp, to make sure it gets serialized correctly by json-serialize into a JSON Array of string.

Using the above snippet, when visiting a *.go file in the project, gopls will automatically build using the provided build tags.

GoGuru scope in emacs

GoGuru is an amazing tool and can be really useful when exploring a project, setting up the scope can be a bit of a pain so as the default function doesn’t even support autocompleting. After some quick hacking here is the first attempt to make at least allow for some easier setup, taking the current file and assuming it is the main package, this little function will setup the scope relative to this file.

https://gist.github.com/sideshowcoder/0d37c53bbf1d62299600bb723cc20af0.js