On Monday, September 12, 2016 at 3:41:35 PM UTC+2, adon...@google.com wrote: > > On Monday, 12 September 2016 08:57:15 UTC-4, Markus Zimmermann wrote: >> >> 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. >> > > I suspect the answer is: because 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. Cheers, Markus -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.