Usage of "for" in sync/map.go

hi,
the functions: tryStore:170, tryLoadOrStore:240, delete:297 in https://github.com/golang/go/blob/master/src/sync/map.go have for {} ,could anyone tell me the reason to do that

Hi @xingkong,

I guess your question is about why these loops have no condition, correct?

The loop bodies contain if conditions that replace the loop conditions. It’s a matter of readability I guess. If there are two or three exit conditions for a loop, it might be more readable to have them as separate if statements inside the loop, rather than building a long, complicated for condition expression.

4 Likes

Thank you a lot

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.