Toggle ‘declared and not used’ errors in Go by using idiomatic _ = notUsedVar
and leaving a TODO comment.
go install github.com/looshch/gouse@latest
Caution
Non-stable (archived) Go versions require older gouse
version.
Go version | gouse version |
---|---|
1.21.0+ | 1.3.0+ |
1.18–1.20.14 | 1.2.3 |
By default, gouse
accepts code from stdin or from a file provided as a path
argument and writes the toggled version to stdout. ‘-w’ flag writes the result
back to the file. If multiple paths provided, ‘-w’ flag is required.
$ gouse
...input...
notUsed = true
...input...
...output...
notUsed = true; _ = notUsed /* TODO: gouse */
...output...
$ gouse main.go
...
notUsed = true; _ = notUsed /* TODO: gouse */
...
$ gouse -w main.go io.go core.go
$ cat main.go io.go core.go
...
notUsedFromMain = true; _ = notUsedFromMain /* TODO: gouse */
...
notUsedFromIo = true; _ = notUsedFromIo /* TODO: gouse */
...
notUsedFromCore = true; _ = notUsedFromCore /* TODO: gouse */
...
First it tries to remove previously created fake usages. If there is nothing to remove, it tries to build an input and checks the build stdout for ‘declared and not used’ errors. If there is any, it creates fake usages for unused variables from the errors.
- Vim: just bind
<cmd> w <bar> silent !gouse -w %<cr>
to some mapping. - Visual Studio Code plugin.
Inspired by Nikita Rabaev’s idea.