That is: prog.go:8: redundant and: true && true prog.go:10: redundant or: true || true
I got this error message for this code: https://play.golang.org/p/pxCdcudFwk8
yet the program prints. What is the meaning of the error messages. Why is it redundant.
And why does it print anyway?
It’s not an error, it’s a warning. And it’s redundant because it will stop evaluation as soon as the result is known.
Since you used constants, false && whatever
is know to always been false, and go will never look at whatever
, similar is for ||
and the first argument beeing true
.
This is called Short-circuit evaluation:
https://en.wikipedia.org/wiki/Short-circuit_evaluation
Please check the following GoLang code to see whether the other operand will be evaluated or not:
https://play.golang.org/p/9sBTnHm3Rtq
BTW: I am wondering why is there no “redundant” message for true || false
and false && true
Probably because go vet
only checks the LHS.
Sorry, I cannot undstand your answer, even though I read it carefully several times.
Interesting.
Lets consider the RHS beeing a function call:
true || createFileAndReturnTrueIfItAlreadyExisted("foo.txt")
This line of code will trigger the warning, but never create a file, due to short circuit evaluation, therefore the ||
and its RHS is redundant, you can remove it, and the observable effect of the program will be the same.
There are 2 cases there.
The first one, where you have true && true
, is redundant because logically (and on a more general approach) p && p
is equivalent to p
, so the second p
is redundant. In this case it doesn’t have to do with p
being true
or false
. Here’s an example where you’ll get the redundant warning when evaluating a && a
, but not when evaluating a && b
, though a
and b
are both true
. Check it at the playground: https://play.golang.org/p/dzWSf9FEPSU
The second case is the one Norbert mentions. Logically, the formula true || (whatever else)
is trivially true
, while false && (whatever else)
is trivially false
, and there’s no need to evaluate the rest of the expression, making it redundant.
Read this again, and for some reason, I got it better this time.
In my last reply, I was answering NobbZ on his first response. I also took a look at 100p1n6 information again, and I understood it a lot better this time, but not totally. I just ran out of time, but I will come back to this, hopefully tomorrow.
What is RHS?
What is a function call?
Can you tell me what I’m to do with the line that is highlighted in yellow?
However I think I understand the basic concept.
Short for right hand side. It usually is used as a name for the right operand of a binary operation. The left operand is often called LHS.
A call to a function as in fmt.Println("foo")
. But if you have to ask this, you really should do the tour again and again. This is fundamental.
What exactly do you mean? Where is a line highlighted in yellow.
Thanks for the first two answers. I will take the tour right after this reply.
As for the third, sorry that I wasn’t more clear. I didn’t know how to express my question.
In many person’s replies, I will see this sort f thing:
true || createFileAndReturnTrueIfItAlreadyExisted(“foo.txt”)
It is printed in a yellow “box”.
I don’t understand what to do with such.
Those are code examples. You are meant to read them at least, sometimes they contain code that you are meant to run, sometimes they provide patches for code snippets you posted earlier, sometimes they are just there.
What to do with them depends on the context of the post they are part of.
In that particular case, it was just an example to read for you, that should explain that the function with the obvious name would never be called, because true || anything
is always true, regardless what anything
is.
Eureka! I understand what you said!
That is such a great feeling!
Fun and interesting! Fun because I understand what you said , and interesting because I just love Go!
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.