GitHub - vipkek/gouse: Toggle ‘declared and not used’ errors in Go.

2 min read Original article ↗

Toggle ‘declared and not used’ errors in Go by using idiomatic _ = notUsedVar and leaving a TODO comment. a demo

Installation

go install github.com/looshch/gouse/v2@latest

Usage

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.

Examples

$ 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 */
...

How it works

gouse first builds the input and checks the build output for ‘declared and not used’ errors. If there are any, it creates fake usages for the reported unused variables. If there are none, it removes previously created fake usages.

Integrations

Credits

Inspired by Nikita Rabaev’s idea. Influenced by Michael Samoilenko.