#Strange behavior when comparing int

6 messages · Page 1 of 1 (latest)

ripe elbow
#

Hi,

I use the package https://github.com/urfave/cli, I write this:

keySize := cliContext.Int("key-size")

Later, I compare this variable

if keySize != 3072 || keySize != 4096 {
    log.Warnln("RSA key size must be 3072 or 4096")
}

Then, when I launch my command, I have this output:

$ go run main.go csr --common-name test --key-size 4096
WARN[2022-09-20 09:55:04] RSA key size must be 3072 or 4096

keySize is equal to 4096, why my code have this behavior?

thorny musk
#

it's an erroneous if statement

#

if the size is 4096 then the condition if keySize != 3072 is true

#

and your saying if the keysize isnt 3072 OR the keysize isnt 4096

#

you would want && instead of || to make it if they keysize isnt 3072 AND the keysize isnt 4096 then it must be some other value

ripe elbow
#

Oh, yes...