bitwise or on bool ?

1,721 views
Skip to first unread message

maxpow...@gmail.com

unread,
Jan 23, 2015, 9:35:16 AM1/23/15
to golan...@googlegroups.com
I need to run a whole bunch of type assertions in sequence to make sure I have actual strings in these values and then panic at the end if anyone of them failed.

My code is pretty straight forward

valn, okn := someval.(string)

This is repeated for several expected values and the result is ok1..okn.
If any ok is false then we need to panic.

I tried what seemed the most logical

if(!ok1|ok2|ok3...){
 panic()
}

But that throws an error

(operator | not defined on bool)


Umm ok, let's find the bitwise or operator, and turns out it's |= because go wanted to be different I guess


if(! ok1|=ok2...){

panic()

}


But it's still the same issue.

Short of digging a hole 10 or so if statements deep, what is the idiomatic was of checking a list of bools in go?


Thanks!



chris dollin

unread,
Jan 23, 2015, 9:55:00 AM1/23/15
to maxpow...@gmail.com, golang-nuts
On 23 January 2015 at 14:35, <maxpow...@gmail.com> wrote:
> I need to run a whole bunch of type assertions in sequence to make sure I
> have actual strings in these values and then panic at the end if anyone of
> them failed.
>
> My code is pretty straight forward
>
> valn, okn := someval.(string)
>
> This is repeated for several expected values and the result is ok1..okn.
> If any ok is false then we need to panic.
>
> I tried what seemed the most logical
>
> if(!ok1|ok2|ok3...){
> panic()
> }
> But that throws an error
>
> (operator | not defined on bool)

The boolean (short-circuit) operator is ||.

(Also note that you're going to want !(ok1 || ok2 ...).

> Umm ok, let's find the bitwise or operator, and turns out it's |=

No, it's |. What made you think it was |=?

> because go wanted to be different I guess

The || vs | distinction is present in C (and indeed in the
ancestor language BCPL) and Java and Javascript and ...

Chris

--
Chris "allusive" Dollin

James Bardin

unread,
Jan 23, 2015, 9:56:42 AM1/23/15
to golan...@googlegroups.com, maxpow...@gmail.com
You want logical operators, not bitwise.

When in doubt, check that language spec page.

maxpow...@gmail.com

unread,
Jan 23, 2015, 10:07:12 AM1/23/15
to golan...@googlegroups.com, maxpow...@gmail.com
Thought I did that already, probably just too much code and not enough caffeine.
Thanks though!

FYI I got the bad info from reddit which was the first search result for "bitwise OR golang"

Specifically here... http://www.reddit.com/r/golang/comments/1w7lw2/what_does_do/

I should know better than to trust reddit for advice though :)

My solution ended up being
ok := ok1 && ok2 ...

if !ok {
 panic()
}

Looked cleaner to me.

chris dollin

unread,
Jan 23, 2015, 10:13:09 AM1/23/15
to maxpow...@gmail.com, golang-nuts
On 23 January 2015 at 15:07, <maxpow...@gmail.com> wrote:

>
> FYI I got the bad info from reddit which was the first search result for
> "bitwise OR golang"
>
> Specifically here...
> http://www.reddit.com/r/golang/comments/1w7lw2/what_does_do/

First comment says:

<<<<<<<<<<<<<<<<<<<<<<
"x |= y" is equivalent to "x = x | y", just like "x += y" is
equivalent to "x = x + y".

| is the bitwise OR operator.

It's the same in C, C++, Java, C#, JavaScript, etc.

Michael Jones

unread,
Jan 23, 2015, 11:02:30 AM1/23/15
to chris dollin, maxpow...@gmail.com, golang-nuts
Minor tangential coding comment. If you test a bunch of things you can distribute the "all" test along with the individual tests.

ok := true // ever the optimist

ok = ok && !someval.(string1) // test 1
ok = ok && !someval.(string2) // test 2
:
ok = ok && !someval.(stringN) // test N

return ok

This makes adding new tests, or commenting some out, or whatever a single-edit local operation rather that a new test and a new "&& ok73" at the end, and also, allows shortcutting of further tests should they be expensive.

If even the if-tests are too expensive (which i doubt) then a predicated version is what you want:

ok := true // ever the optimist

if ok {
ok = ok && !someval.(string1) // test 1
if ok {
ok = ok && !someval.(string2) // test 2
:
if ok {
ok = ok && !someval.(stringN) // test N
}
:
}
}

return ok
 A looping version of this, with test strings in a slice and a "while" sense to the loop is what I would expect in real code;

ok := true
for _, s := range SliceOfTestStrings {
    if !ok { break }
    ok = ok && !someval.(s)
}
return ok

None of this makes much difference, I just feel like shaking my cane.


--
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.



--
Michael T. Jones | Chief Technology Advocate, Google  | m...@google.com |  +1 650-335-5765

maxpow...@gmail.com

unread,
Jan 23, 2015, 11:25:08 AM1/23/15
to golan...@googlegroups.com, ehog....@googlemail.com, maxpow...@gmail.com
Yeah you're right except about slicing and dicing.  I expect to convert to that soon, but when I started out, I just wanted to check a couple of specific things and then the list kept growing.  :)

Marvin Renich

unread,
Jan 23, 2015, 11:53:58 AM1/23/15
to golang-nuts
* 'Michael Jones' via golang-nuts <golan...@googlegroups.com> [150123 11:02]:
> Minor tangential coding comment. If you test a bunch of things you can
> distribute the "all" test along with the individual tests.
>
> ok := true // ever the optimist
>
> ok = ok && !someval.(string1) // test 1
> ok = ok && !someval.(string2) // test 2
> :
> ok = ok && !someval.(stringN) // test N
>
> return ok

To be pedantic, I think you meant someval1.(string), someval2.(string),
etc. Also, type assertion returns the value and, as an optional second
result, the bool you are looking for, so it would have to be something
like

var okay bool
_, okay = someval1.(string) ; ok = ok && okay
_, okay = someval2.(string) ; ok = ok && okay

...Marvin

Michael Jones

unread,
Jan 23, 2015, 11:56:39 AM1/23/15
to Marvin Renich, golang-nuts
yes. absolutely. i wondered if i'd been to quick to reply before getting into the shower. ;-)
thanks!

--
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.

Egon

unread,
Jan 23, 2015, 3:55:51 PM1/23/15
to golan...@googlegroups.com
Where do you get the initial string values? You might be better off collecting problems there. e.g.

conv := Converter{object}
a = conv.Str("hello")
b = conv.Str("world")
...
if conv.Err != nil {
return conv.Err
}
// a, b can now be directly used

It shouldn't be hard to figure out how to write such converter.

+ Egon

Lars Seipel

unread,
Jan 24, 2015, 12:35:07 AM1/24/15
to maxpow...@gmail.com, golan...@googlegroups.com
On Fri, Jan 23, 2015 at 06:35:16AM -0800, maxpow...@gmail.com wrote:
> valn, okn := someval.(string)
>
> This is repeated for several expected values and the result is ok1..okn.
> If any ok is false then we need to panic.

Just drop the "ok" and you also get a panic if the type assertion
doesn't hold.

s := v.(string) // panics if v doesn't have dynamic type string

You might want to write your code in a way that lets the compiler do the
type checking, though.
Reply all
Reply to author
Forward
0 new messages