unused import and variables, necessary at compiler level? Experience can be improved?

845 views
Skip to first unread message

Alex Buchanan

unread,
Jan 31, 2018, 9:46:40 PM1/31/18
to golang-nuts
Unused variables and imports are strictly disallowed by the Go compiler. Most of the time, I enjoy this benefit. These days, I've started to notice how often things go dead and/or unused in my python code.

But, sometimes, this behavior is incredibly annoying. If I'm trying to debug something by commenting out lines, or if I'm hacking up an experimental script (meant to run with `go run`), I find this checking is very unwanted. For example, if I comment out one variable, it may lead to a new set of variables being unused, which sometimes leads back to a package being unused. It's not uncommon for this cycle to take 3-5 iterations, in my experience.

Quickly hacking up Go scripts would be improved by relaxing this constraint, in my opinion.

Is finding a better compromise at all interesting or even possible? Are parts of the compiler written to depend on the fact that variables are definitely used?

My idea for a potential improvement (again, having zero knowledge of the compiler requirements) is to move this checking to vet.

Thanks!

Jan Mercl

unread,
Feb 1, 2018, 2:35:37 AM2/1/18
to Alex Buchanan, golang-nuts
On Thu, Feb 1, 2018 at 3:46 AM Alex Buchanan <buchan...@gmail.com> wrote:

Put this line into any of the package *_test.go files:

        func use(...interface{}) {}

When disabling some code in non-test files leads to unused variables foo and baz error, insert `use(foo, bar)` there to keep the compiler happy. You cannot forget the use invocation in production code as it is defined only when `go test` is building the program.

I have it included in my all_test.go template.
--

-j

Elazar Leibovich

unread,
Feb 1, 2018, 3:45:50 AM2/1/18
to golang-nuts
While Jan's tip sounds a better idea, I made a small piece of code that rewrites your code AST to use every unused variable and run it temporarily:


It might not work now for new Go syntax, but it worked for me in the past, and you might be able to update it.

wright...@gmail.com

unread,
Feb 1, 2018, 10:26:14 AM2/1/18
to golang-nuts
You can also use _ to store unused variables. Like this:

Jan Mercl

unread,
Feb 1, 2018, 10:34:07 AM2/1/18
to wright...@gmail.com, golang-nuts
On Thu, Feb 1, 2018 at 4:26 PM <wright...@gmail.com> wrote:

> You can also use _ to store unused variables. Like this:

That works, but I do not recommend to do that. The temporary '_= foo' workaround is too easy to miss and it can stay accidentaly left in production code. The earlier discussed 'use(foo)' avoids it happening.

--

-j

loui...@gmail.com

unread,
Feb 1, 2018, 11:19:04 AM2/1/18
to golang-nuts
I have my editor set up to hilight "// DEBUG", so I tend to do:

> _ = foo // DEBUG

Problem solved. :)

Alex Buchanan

unread,
Feb 1, 2018, 12:00:23 PM2/1/18
to golang-nuts
Clever workarounds!

These solutions don't handle the case of commenting out a function call, which is a common need IMO. Whatever the workaround, it would still be easier if done by vet, right?

marwan....@nytimes.com

unread,
Feb 1, 2018, 3:36:38 PM2/1/18
to golang-nuts
I usually wrap the function call with `if false` i.e: 

if false {
  package.Function()
}

This "comments out" the function call and keeps the package imported. 
Reply all
Reply to author
Forward
0 new messages