Doing "C-like" things in Go

378 views
Skip to first unread message

CampNowhere

unread,
Sep 11, 2017, 8:07:48 PM9/11/17
to golang-nuts
I am a C developer and am trying to pick up Go.

My question is this. C doesn't "care" about truthfulness, it just cares about zero and non-zero when evaluating a logical AND operator. So something like the following in C is totally kosher:

int a = 10;
int b = 20;

while(a && b)
{
    do_something
();
}

However, Go requires blloean values used with the logical AND operator ... so my necessary code change for Go implementation becomes the following:

var a,b int = 10,20

for (a != 0) && (b != 0) {
    do_something
()
}

Is doing things this way absolutely necessary? It seems a lot clunkier and less elegant.

Shawn Milochik

unread,
Sep 11, 2017, 8:13:05 PM9/11/17
to golang-nuts
Go is more explicit than C. Mistakes won't compile. Perfectly valid syntax in C could be a subtle logic bug; Go tries to avoid these.

Michael Jones

unread,
Sep 11, 2017, 9:25:54 PM9/11/17
to Sh...@milochik.com, golang-nuts
That said:

int(boolvalue) could mean 0 for false and 1 otherwise
bool(intvalue) could mean false for 0 an true otherwise

quite a useful notion

On Mon, Sep 11, 2017 at 5:12 PM, Shawn Milochik <shawn...@gmail.com> wrote:
Go is more explicit than C. Mistakes won't compile. Perfectly valid syntax in C could be a subtle logic bug; Go tries to avoid these.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Michael T. Jones
michae...@gmail.com

Konstantin Khomoutov

unread,
Sep 12, 2017, 4:59:57 AM9/12/17
to CampNowhere, golang-nuts
I disagree. Go approach is a lot clearer.

Why 10 is true? Why -42 is true? Why a float NaN is true?

That can even boild down to reading the code as plain English.
"while a and b do something" is of dubious readability compared to
"while a is not zero and b is not zero do something".

Scott Pakin

unread,
Sep 18, 2017, 2:29:29 PM9/18/17
to golang-nuts
On Monday, September 11, 2017 at 7:25:54 PM UTC-6, Michael Jones wrote:
int(boolvalue) could mean 0 for false and 1 otherwise
bool(intvalue) could mean false for 0 an true otherwise

quite a useful notion

 Yeah, too bad that was shot down years ago (cf. proposal: support int(bool) conversions in Go.1.5).

— Scott

as

unread,
Sep 18, 2017, 2:55:30 PM9/18/17
to golang-nuts
There's always a way to write confusing code in any language.


var
 a,int = 10,20

for a&b != 0 {
    do_something
()

Sokolov Yura

unread,
Sep 19, 2017, 12:13:12 AM9/19/17
to golang-nuts
for a|b != 0 {
do_something()

roger peppe

unread,
Sep 19, 2017, 8:06:55 AM9/19/17
to CampNowhere, golang-nuts
The required syntax is actually marginally simpler:

for a != 0 && b != 0 {
}

I very much like Go's explicit use of booleans. One advantage is that
you always know the exact value of any true/false value, so you
can be sure that:

if a && b {
if a == b {
print("ok")
}
}

will always print "ok". In C it may not - you need to remember to do:

if((a != 0) == (b != 0)){

which is sometimes easy to forget.

Having explicit bools makes the language cleaner and easier to
understand IMHO.

That doesn't mean I'm against having an explicit bool to int conversion,
though.

>
> --
> 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.
Reply all
Reply to author
Forward
0 new messages