(And "imported but not used" errors.)
No. It's a FAQ:
http://weekly.golang.org/doc/go_faq.html#unused_variables_and_imports
No, there is not such an option. It has been discussed several times in
this list.
You can use the blank identifier to avoid this issue:
To avoid unused imports:
var _ = fmt.Printf // package fmt is now used
To avoid unused variables:
var _ = foo // foo is now used
These changes have little impact in the rest of the code
M;
> Is there a flag that enables you to temporarily compile without this
> warning?No, there is not such an option. It has been discussed several times in
this list.
You can use the blank identifier to avoid this issue:
>> Is there a flag that enables you to temporarily compile without this
>> warning?
--
Michael T. Jones | Chief Technology Advocate | m...@google.com | +1
650-335-5765
How can the lack of warnings slow down development?
--
Aram Hăvărneanu
Because unused variables are errors, so you /have/ to do something
to your code to squelch them, rather than sorting out whatever
your current problem is -- after which there will likely be no
unused variables left, problem solved.
So it is said, and I have sympathies for this argument.
Chris
--
Chris "allusive" Dollin
On 23 March 2012 08:20, Aram Hăvărneanu wrote:
>> The lack of warnings slows down development and many people have
>> already complained about it.
>
> How can the lack of warnings slow down development?Because unused variables are errors, so you /have/ to do something
to your code to squelch them, rather than sorting out whatever
your current problem is -- after which there will likely be no
unused variables left, problem solved.
So it is said, and I have sympathies for this argument.
PS: Yes, I believe warnings in this case would only slow down my development productivity both in the average of the long run and also right now as they would allow me to postpone the problem to a point when it's harder to solve it.
... and it is this process that introduces the (temporarily) unused
variables and causes the compiler to put your brakes on.
At this point, dealing with unused variables is make-work,
not productive effort.
I don't want to export code with unused variables past this
work session. But I don't want to be sticking in assigments
to _ or commenting out imports or deleting swathes of code
/just/ to deal with a problem that will almost certainly deal
with itself in the next ten minutes.
On Fri, Mar 23, 2012 at 10:59 AM, Jan Mercl wrote:PS: Yes, I believe warnings in this case would only slow down my development productivity both in the average of the long run and also right now as they would allow me to postpone the problem to a point when it's harder to solve it.Warnings is not the only solution for this problem, I'm fine with having unused variables as errors by default. However for some use cases, such the one I described, it makes sense to have an option to, say, go build --force, that would ignore errors when it makes sense. Sometimes, by the time you remove the unused variables error, you could have verify these lines aren't the source of your bug, and comment them in again. Also not having "go build --force" encourages you to add things like "var _ = a" to your code base. You can easily forget to remove this statement, and then have unused variables in your production build.
The FAQ oppose this problem from a purist POV, "flags should not change the semantics of the compiler", I don't see any practical damage it might cause. However, this POV does not help me as a practitioner to find my bugs faster in a certain use cases.
On 23 March 2012 08:59, Jan Mercl wrote:
> Because unused variables are errors I *have* to do something to my code to
> squelch them (before the compiler accepts is), so I must sort out what
> problem my code has (right now, when I freshly wrote it and not a year
> later)... and it is this process that introduces the (temporarily) unused
variables and causes the compiler to put your brakes on.
At this point, dealing with unused variables is make-work,
not productive effort.
> It seems to me that I get an "unused variable" error at a reasonably low
> rate, usually right after adding a new func/method (followed habitually by a
> test drive compiling of the package only to check the compiler can still
> cope with it) when I make a silly logic/typing error. Then such error is
> welcome and it's fixed most times within seconds. But I appreciate I'm
> effectively forced to make the fix immediately and not allowed to be lazy
> now - at the cost of future headaches ;-)
I usually get them when I'm modifying existing code and
wish to put in debugging prints or alternative paths,
where the variables [or package names] may dance in
and out of usedness as I work out what's going on.
EG the fmt dance where `import "fmt"` comes and goes as
fmt.PrintXXX calls appear and disappear.
The FAQ oppose this problem from a purist POV, "flags should not change the semantics of the compiler", I don't see any practical damage it might cause. However, this POV does not help me as a practitioner to find my bugs faster in a certain use cases.It seems to me that I get an "unused variable" error at a reasonably low rate, usually right after adding a new func/method (followed habitually by a test drive compiling of the package only to check the compiler can still cope with it) when I make a silly logic/typing error. Then such error is welcome and it's fixed most times within seconds. But I appreciate I'm effectively forced to make the fix immediately and not allowed to be lazy now - at the cost of future headaches ;-)
Also, if you use fmt.Println for debugging, you can add a source file
that assigns it to a name (e.g. debugln) so that you can use it easily
without worrying about importing fmt.
Rémy
Rémy
On Friday, March 23, 2012 10:06:18 AM UTC+1, Elazar Leibovich wrote:_ = a // is shorterAnd still I wouldn't recommend doing this. Just fix the error. Now, not later.
This is one of those things that seems foreign and weird to new Go programmers, but is really a non-issue once you start writing some code. Same with imports. You really need to just accept it, and then find out that your projects a much slower rate of code-rot than they normally do :)
if i find myself adding and removing a particular package a lot,
i'll sometimes do something like:
var _ = log.Printf
which means i don't have to delete the import when i remove the last
use of that package.
I don't suffer from that even though I write complex apps. The only
case where I can find it annoying is in range loops where the range
variables that are actually used may vary. Otherwise, either a
variable is used only once and then it's declared near its usage, so
they are both commented at the same time, either it's used more than
once, and then there's no problem commenting blocks of code either.
Rémy.
+1
“Unused vars“ should be treated as warning, not error. Please stop forcing people do what they don't like. The compiler is a "tool", not "God", of programmers. Warning messages are enough for people to clean unused vars, if they want.
+1
“Unused vars“ should be treated as warning, not error. Please stop forcing people do what they don't like. The compiler is a "tool", not "God", of programmers. Warning messages are enough for people to clean unused vars, if they want.
On Fri, Mar 23, 2012 at 7:16 PM, Liigo Zhuang <com....@gmail.com> wrote:+1
“Unused vars“ should be treated as warning, not error. Please stop forcing people do what they don't like. The compiler is a "tool", not "God", of programmers. Warning messages are enough for people to clean unused vars, if they want.If it's worth making a warning, it's worth making an error.
The point is, i think, that such errors disrupt the flow of prototyping and debugging, while warnings do not. Once things have settled, one can fix the warnings. I guess everyone likes to see "0 errors, 0 warnings" in the end?
I don't think this is a good idea. You might forget to remove this var _, and debugging code will end in production.
I'm OK with the current setup, but it might help to convince others if anyone can give a really good real-life example of why allowing unused vars is such a bad thing?
Or why even optionally allowing them is such a bad thing?
And why is it so important to not allow unused variables, but at the same time it's fine to allow (private) unused functions or types?
On Sunday, March 25, 2012 8:04:02 AM UTC+13, Elazar Leibovich wrote:I don't think this is a good idea. You might forget to remove this var _, and debugging code will end in production.
[...]
When using these tricks, you will probably find that you hardly need to debug (in the traditional sense) much at all.
> But my point is, I don't see any damage with leaving this option behind a
> switch, and I see benefit to many people in the community.
The source is all there for you to try your idea out. I think the
discussion has run on longer than making the change would take.
?g are not gcc. It's a very compact compiler and it rewards inspection
... why not just give it a go, make your desired change, and let us
know how it works out?
I suspect you'll find it goes well in the early going, and it bites
you later. Every time I've seen this type of shortcut that's they way
it worked out.
thanks
ron
Almostly NO other compilers did this (refuse unused vars ). It's crazy.
"Crazy" is a bit strong. "Sometimes inconvenient. occasionally
a pain" comes closer.
Unused variables and imports are so much more often a mistake
than not that it seems useful that they're rejected.
Apart from those times of flux when the code in the box may or may
not be dead, I don't want unused variables in my code. (I can't
remember why Go doesn't complain about unused nonexported
functions ...)
Almostly NO other compilers did this (refuse unused vars ). It's crazy.
Error messages make you happy, and warning messages make me happy. An optional switcher of compiler will make all gophers happy. So I don't know why you guys oppose this so strongly. This is why I think someone maybe crazy.
It would make me unhappy. First in principle and then again when I
notice the first project that tells me you have to build it with that
optional switch.
> Is there a flag that enables you to temporarily compile without this
> warning?No, there is not such an option. It has been discussed several times in
this list.
You can use the blank identifier to avoid this issue:
To avoid unused imports:
var _ = fmt.Printf // package fmt is now used
To avoid unused variables:
var _ = foo // foo is now used
These changes have little impact in the rest of the code
M;
If a programmer is not diligent enough to removing warnings when they are presented to him in a list at compile time then how is he supposed to be diligent enough to go through his code and remove all the empty assignments?
but why not check for unused function parameters? That is no less a sign of a programmer error than unused local variables, right?
- Aaron
The purpose of no warnings, I assume, is to avoid sloppy code. If there are a bunch of "var _ = foo" statements scattered about as workarounds to the no warning problem, then I'd say no warnings has failed miserably. If a programmer is not diligent enough to removing warnings when they are presented to him in a list at compile time then how is he supposed to be diligent enough to go through his code and remove all the empty assignments?
I sometimes have a very strange error, which I can't understand its source.One of my techniques for finding it, is isolate the problem to a very small case, by commenting out pieces of the code, and noticing which pieces of codes cause the problem.Go really helps me with it due to the short compile run cycles, but it is very difficult to use this technique without leaving unused variables.
--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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
package main
func unused(x interface{}) {}
func main() {
a := 1
unused(a)
}
2012년 3월 7일 수요일 오후 8시 32분 19초 UTC+9, Elazar Leibovich 님의 말:I sometimes have a very strange error, which I can't understand its source.One of my techniques for finding it, is isolate the problem to a very small case, by commenting out pieces of the code, and noticing which pieces of codes cause the problem.Go really helps me with it due to the short compile run cycles, but it is very difficult to use this technique without leaving unused variables.Is there a flag that enables you to temporarily compile without this warning?
--
On Thu, Jan 10, 2019 at 6:04 PM 김용빈 <kyb...@gmail.com> wrote:package main
func unused(x interface{}) {}
func main() {
a := 1
unused(a)
}The function isn't even required here. Assigning to underscore will prevent the error:func main() {
a := 1_ = a
}
-j