Re: [go-nuts] suspect or: role != admin || role != superadmin

2020-05-11 Thread Ian Lance Taylor
On Mon, May 11, 2020 at 11:53 AM Saied Seghatoleslami
 wrote:
>
> I have seen a ticket on this topic (or something related to it) on Github but 
> I thought I would ask this community first:
>
> In first instance, I got "suspect or: role != admin || role != superadmin"
> In the second instance, it works just fine.  I have verified that role is 
> string type (with %T verb).
>
> since role can be admin, superadmin, or anything else for that matter, why 
> would the first case generate the suspect message, and the second case works 
> fine.
>
> I am running go version go1.13.4 darwin/amd64 on a Mac
>
>
> First Instance:
>
> const (
>  admin  = "admin"
>  superadmin = "superadmin"
> )
>
> invalidRole := role != admin || role != superadmin

This sets invalidRole if role != admin or if role != superadmin.
Let's say role is admin.  Then role != superadmin is true, so
invalidRole is set to true.  Let's say role is superadmin.  Then role
!= admin is true, so invalidRole is set to true.  Let's say role is
frobozz.  Then role != admin is true, so invalidRole is set to true.
In other words, invalidRole is always set to true.  This is a "suspect
or".


> Second instance:
>
> const (
>  admin  = "admin"
>  superadmin = "superadmin"
> )
>
> validRole := role == admin || role == superadmin

There is no such confusion here.

Ian

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXuZNvdo2nYz4wmCpberzG2obeHDv87ThRZjdAipQrkTA%40mail.gmail.com.


Re: [go-nuts] suspect or: role != admin || role != superadmin

2020-05-11 Thread Kurtis Rader
The inverse of

  validRole := role == admin || role == superadmin

is

  invalidRole := role != admin && role != superadmin

or

  invalidRole := !(role == admin || role == superadmin)

It is not

  invalidRole := role != admin || role != superadmin

Think about it. No matter what value `role` has it has to be unequal to at
least one of those constants and thus that expression is always true.

On Mon, May 11, 2020 at 11:53 AM Saied Seghatoleslami <
seghatoleslam...@gmail.com> wrote:

> I have seen a ticket on this topic (or something related to it) on Github
> but I thought I would ask this community first:
>
> In first instance, I got "suspect or: role != admin || role != superadmin"
> In the second instance, it works just fine.  I have verified that role is
> string type (with %T verb).
>
> since role can be admin, superadmin, or anything else for that matter, why
> would the first case generate the suspect message, and the second case
> works fine.
>
> I am running go version go1.13.4 darwin/amd64 on a Mac
>
>
> First Instance:
>
> const (
>  admin  = "admin"
>  superadmin = "superadmin"
> )
>
> invalidRole := role != admin || role != superadmin
>
>
>
> Second instance:
>
> const (
>  admin  = "admin"
>  superadmin = "superadmin"
> )
>
> validRole := role == admin || role == superadmin
>
>
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/ef2e9e72-0f14-49d9-99fd-950bab0b5a41%40googlegroups.com
> 
> .
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD9uxjDk2xGC8ATamT8mK_Wv7RhqQYgtm8VQMSH0vrym%3DA%40mail.gmail.com.


[go-nuts] suspect or: role != admin || role != superadmin

2020-05-11 Thread Saied Seghatoleslami
I have seen a ticket on this topic (or something related to it) on Github 
but I thought I would ask this community first:

In first instance, I got "suspect or: role != admin || role != superadmin"
In the second instance, it works just fine.  I have verified that role is 
string type (with %T verb).

since role can be admin, superadmin, or anything else for that matter, why 
would the first case generate the suspect message, and the second case 
works fine.

I am running go version go1.13.4 darwin/amd64 on a Mac


First Instance:

const (
 admin  = "admin"
 superadmin = "superadmin"
)

invalidRole := role != admin || role != superadmin



Second instance:

const (
 admin  = "admin"
 superadmin = "superadmin"
)

validRole := role == admin || role == superadmin



-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ef2e9e72-0f14-49d9-99fd-950bab0b5a41%40googlegroups.com.