So, I've been writing Go for some years now, and while I know there is more to the for loop than the "usual" day-to-day case, this, I just don't quite get what is going on.
I mean, I can use it, but I don't get it.
for key, _, ok := it.Next(); ok; key, _, ok = it.Next()
It is from the immutable radix tree package here: https://github.com/hashicorp/go-immutable-radix.
I actually ask, because to retrieve the data I put in the tree, I have altered the code slightly () _ to blob), but now I am not sure if I should do the same in the second part.
for key, blob, ok := it.Next(); ok; key, _, ok = it.Next()
CodePudding user response:
The loop starts with calling it.Next()
, and continues as long as ok
is true. ok
is reset with it.Next()
at the end of every loop. At each iteration, the loop also sets key
.
Your modification sets blob
at the initialization, but it never sets blob
again. For all iterations, blob
will have the first value it got from it.Next()
. It is likely that you should set blob
at the last condition of the loop as well, so you can get the blob
for the next element.