Unused variables will block compiling (that's a good thing) but compiler don't really care about unused functions, is there an explanation ?
-
You are not using them does not mean another consumer of your class will not. Unused variables don't by default block compiling. It's a warning as far as I know. – Oguz Ozgul Nov 17 '15 at 09:39
-
@OguzOzgul You're right but, in a main package ? – Mistermatt Nov 17 '15 at 10:06
-
This question is not a duplicate of the originally marked one ([this](http://stackoverflow.com/questions/22549228/why-does-golang-allow-compilation-of-unused-function-parameters), as it's about unused function _parameters_ and not the _functions_ themselves), but this is a possible duplicate of [Why Does Golang Allow Compilation of Unused Functions?](http://stackoverflow.com/questions/32564935/why-does-golang-allow-compilation-of-unused-functions). – icza Nov 17 '15 at 11:10
-
@OguzOzgul Unused variables is a compile-time _error_, not a warning. There are no compiler warnings in Go. And if a package has an unexported function which if the package itself doesn't call, noone else will as noone else can reach that (not even with reflection). – icza Nov 17 '15 at 11:15
-
Please forgive my dumbness for commenting on this question as if it was tagged c#. I don't know what is wrong with me :) – Oguz Ozgul Nov 17 '15 at 13:36
-
Not sure why the downvotes – although short, it's a completely valid question. Very healthy curiosity. – tomasz Nov 17 '15 at 14:08
-
@tomasz thanks for the comment (: – Mistermatt Nov 17 '15 at 15:59
1 Answers
The behaviour seems consistent between variables and functions – both are allowed in a package-level scope, even if unused.
This snippet is compiling without any issues:
package main
var v int // unused variable
func f() {} // unused function
func main() {}
Now, when it comes to local scope things are different and unused variables generate errors. Same for function literals (named nested functions are not allowed in Go):
func main() {
func() {}
}
// Error: func literal evaluated but not used
Finally, why is only local scope checked for unused variables? Because usually it's a bug (for examples, in Go, due to an accidental usage of :=
). And compiler saved me many times here. Consider this:
func f() (err error) {
if somthing {
err := g() // err is unused variable! I really meant `=`.
}
return
}
For global (package-level) scopes unused variables and functions are usually just polluting the namespace as, for example, someone forgot to remove them after refactoring. There're some tools which helps detecting these, for example:
- https://github.com/opennota/check
- https://github.com/alecthomas/gometalinter (uses the previous package)
- https://github.com/remyoudompheng/go-misc/blob/master/deadcode/deadcode.go
I also found a post by Russ Cox commenting on the issue:
I have been writing some C code with gcc -Wall -Werror recently. It makes prototyping code somewhat difficult to be told "you didn't use that function, go fix it" when you're just trying to test what you've got so far, or commenting out calls that are possibly causing trouble. Of course the same is true for warning about unused local variables too. The difference is that, in Go, because of :=, an unused local variable is very often a bug, while an unused unexported function is much less often a bug.
Russ

- 12,574
- 4
- 43
- 54