Const values can be accesed still by reflection in a reasonable manner. This question has come by quite a few times before on the mailing list, better explanations should be easy to find.
Hi gophers!
I am wondering why we have a "declared and not used" compile error for variables [https://play.golang.org/p/KLzHVO5L7q] but not for constants [https://play.golang.org/p/JG9dSa_VKg]? I am sure there is some rational decision behind this that I have missed, but my Google-fu is just not strong enough to find an answer.
Hello,Const values can be accesed still by reflection in a reasonable manner. This question has come by quite a few times before on the mailing list, better explanations should be easy to find.
Cheers,
Markus
On Monday, September 12, 2016 at 3:41:35 PM UTC+2, adon...@google.com wrote:unused constants and types cost nothing at run time. It's not that simple, of course, because constant and type declarations may be the only uses of an import of a package whose initialization has some cost; but this is quite marginal.
The const declaration of my example is inside an unexported function body. It can not be accessed by an import. So, it should really matter only at compile time, right?
However, I am referring to the definition of https://golang.org/doc/faq#unused_variables_and_imports which states "... Go refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity." Which holds true in the same way for an unused const as it does for an unused variable. However, even though it produces the same results, i.e. a new declaration which decreases the program clarity, only the variable declaration is marked.
Additionally, if an unused constant is compiled into the binary it does also matter to the binary size, even just a little bit.
On 13 September 2016 at 08:22, Markus Zimmermann <zim...@gmail.com> wrote:On Monday, September 12, 2016 at 3:41:35 PM UTC+2, adon...@google.com wrote:unused constants and types cost nothing at run time. It's not that simple, of course, because constant and type declarations may be the only uses of an import of a package whose initialization has some cost; but this is quite marginal.
The const declaration of my example is inside an unexported function body. It can not be accessed by an import. So, it should really matter only at compile time, right?Right. The import case I was referring to is this:import "p"func main() {const unused = p.K}The unused constant causes p to be an (unnecessary) dependency, which in turn causes p.init to be executed.
However, I am referring to the definition of https://golang.org/doc/faq#unused_variables_and_imports which states "... Go refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity." Which holds true in the same way for an unused const as it does for an unused variable. However, even though it produces the same results, i.e. a new declaration which decreases the program clarity, only the variable declaration is marked.You are right that the motivation given in the FAQ should apply equally to constants and types.
Do you think this might be worth bringing up in golang-dev?