0

I've stumbled upon a usage as err == nil != c.Ok. I couldn't make out what it exactly checks for and find documentation as to it on the internet.

It was used in the following snippet as such:

if err := validateMountPath(c.Path); err == nil != c.Ok {
    t.Errorf("path: %v, expected: %v, got: %v", c.Path, c.Ok, err == nil)
}

Could you explain the usage of a == b == c in Go?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
ibrahim koz
  • 537
  • 4
  • 15
  • 8
    Because operators `==` and `!=` have the same [precedence](https://go.dev/ref/spec#Operator_precedence), `err == nil != c.Ok` gets parsed as `(err == nil) != c.Ok`. So it's just comparing the (untyped) boolean result of `err == nil` to `c.Ok`. Nothing particularly specific to Go; comparison operators in many other languages work the same way. – jub0bs Dec 29 '21 at 10:57
  • 2
    It makes sure that `err` and `c.Ok` agree on the result of the operation. – Ulrich Eckhardt Dec 29 '21 at 11:31
  • This branch is entered only when: 1) you have an error and c.Ok is true; 2) when you don't have an error but c.Ok is false. – Stefan Zhelyazkov Dec 29 '21 at 12:46

0 Answers0