Comma ok in loops

383 views
Skip to first unread message

Salvador Girbau

unread,
Dec 24, 2009, 3:13:49 PM12/24/09
to golang-nuts
Hello,
when using the "comma ok" idiom in loops, I find myself unable to use
the loop's condition:

for {
value, ok := Func ();
if value == nil {
break;
}
// ... rest of body
}
// analyze "ok" here if needed

Is there a way to avoid the in-body condition/break, and actually use
the for's condition atop?

Thanks, and merry christmas!

Rob 'Commander' Pike

unread,
Dec 24, 2009, 3:30:51 PM12/24/09
to Salvador Girbau, golang-nuts

That's the pattern - although ok won't be visible outside the loop as
written.

-rob

peterGo

unread,
Dec 24, 2009, 5:35:53 PM12/24/09
to golang-nuts
Salvador,

For your example, try this idiom.

value, ok := commaok()
for value != nil {
// ... rest of value body
value, ok = commaok()
}
if ok == true {
// ... rest of ok body
}

Note that the 2009-12-22 release eliminated most uses of semicolons;
use gofmt to reformat.

release.2009-12-22: http://groups.google.com/group/golang-nuts/msg/ed2c38932ba1bd44

Peter

Maxime Simon

unread,
Dec 24, 2009, 5:58:45 PM12/24/09
to Salvador Girbau, golang-nuts
for {
   value, ok := Func ();
   if value == nil {
       break;
   }
   // ... rest of body
}
// analyze "ok" here if needed

Is there a way to avoid the in-body condition/break, and actually use
the for's condition atop?

Actually you would have the same issue with any C-like
languages. It's a problem of scopes, and the variable "ok"
is in the scope of the for-loop and can't be used outside it.

You may be able to declare a variable outside the for-loop and
then use it to retrieve the returned values:

var ok bool
for {
  var value int
  value, ok = Func()
  if value == nil {
    break
  }
  // ... rest of body
}
// analyze "ok" here if needed

Regards,
--
Maxime

Maxime Simon

unread,
Dec 24, 2009, 6:29:59 PM12/24/09
to Salvador Girbau, golang-nuts
Though Peter's solution is somehow better than mine. ^^

Having a condition for the for-loop instead of a break makes
it a bit more readable. And also you won't have to declare each
variable with the 'var' keyword.

Regards,
--
Maxime

Salvador Girbau

unread,
Dec 25, 2009, 3:43:27 AM12/25/09
to golang-nuts
Thanks to all. I guess the point here is that I'm missing the comma
operator,

var value int
var ok bool

for value, ok = Func(), value != nil {
// ...
}

though I realize that using the comma token itself can be pretty
ambiguous in this context. (Maybe some *other* token.)

Ostsol

unread,
Dec 25, 2009, 11:47:32 AM12/25/09
to golang-nuts

That second comma will not work. The compiler will assume that
'value != nil' is what you are trying to assign to 'ok' and it will
complain that 'Func', which returns two values, is being used in a
single value context. You have to put a semicolon there instead and
follow it with a statement.

var value int;
var ok, bool;

for value, ok = Func (); value != nil; value, ok = Func () {
// ...
}

-Ostsol

Reply all
Reply to author
Forward
0 new messages