Why I Made gouse

Motivation

My friend Ilya during his active study of Go noted that it’s annoying to manage ‘declared but not used’ errors when you’re just testing things out. He had an idea of creating a tool for toggling fake usages for unused variables. He imagined an implementation based on commenting them out. I was thinking about it and came to conclusion that commenting variables out isn’t scalable because several variables may be declared on the same line.

How It Works

I started from what I know is idiomatic: _ := variable. Also I know that semicolon is allowed for code in Go. I’ve checked if trailing ; are being removed by gofmt. Indeed they are. So in the end I had an easy solution: append ; _ = variable to the line where variable is.

gouse first searches for fake usages, removes them and returns. If it doesn’t find them, it tries to compile a file in a temporary directory and looks for ‘no required module provides package’ errors in a build output and comments out lines causing them, if any. Then it tries to compile it again but looks for ‘declared but not used’ errors and applies the solution to associated variables and un-comments commented out lines.

The State of gouse Now

The first half-baked version was written in Lua in form of a Neovim plugin using data from LSP. I shared it with Ilya but he seems not interested in the language anymore. Anyway I wanted to share it with others. To make it scalable I decided to implement the tool back end in Go so it would be easy to integrate it with an editor or IDE of choice. I made a VS Code plugin and wrote a very simple (Neo)vim integration.

I hardly ever use the language since though.