GoLand hits the breakpoint, so one can examine the program’s state and control its execution in an interactive manner. Let’s find out how it’s implemented for Go. The official LSP documentation uses Go to Definition as an example. The next logical step would be to call a command and see what happens. For many commands, it’s easy enough to start here. internal/lsp/server_gen.go is an LSP server entry point. vscode/settings.json looks pretty much basic.
It’s enough to add -remote=:37374 to the list of flags (don’t forget to update the port if you’ve decided to change it for -listen).
The Go plugin for VS Code has a setting go.languageServerFlags that allows passing arguments to gopls. I use Visual Studio Code as the most popular one.Īctually, there’s not much to tell here. The process might look different for different editors. The rest is automatically generated by the IDE.Īfter saving the run configuration with OK, we can launch gopls by choosing Debug ‘go build /…' instead of Modify Run Configuration… Now, we need to teach an editor to start a client version of gopls. In GoLand, one does it by modifying a run configuration. Before launching it, we need to add an argument -listen=:37374 (you can use any other port or even a Unix socket) that tells gopls to act as a server. Let’s start with checking out Go tools and opening gopls/main.go.
What is more important in our case, it’s trivial to launch the server right in the IDE. The former works as a proxy while the latter does all the work. It allows splitting gopls into a client and a server. Fortunately, there’s a better way called a daemon. This makes it harder to attach a debugger to it. While, typically, debugging a program in GoLand is as easy as a couple of clicks, the problem with gopls is that a) it’s launched by an editor b) at an arbitrary moment in time. Gopls is a language server protocol implementation for Go. This time, I’d like to share with you how one can debug gopls using GoLand. In the previous issue of learning by observing, I’ve described how one can debug the Go compiler.