I don't really know Go, but I'm assuming that this behaves like a similar C program where the type of i is uint8_t (unsigned 8-bit byte).
First loop iteration: i is zero. i is post-incremented and becomes 1.
Loop iterations where i is in the range 1..253 blah, blah, blah... we just keep printing and incrementing.
Loop iteration i = 254: i is post-incremented. i is now 255. This is the highest possible value for an 8-bit integer, when interpreted as an unsigned value.
Loop iteration i = 255. The check still passes. The body still executes. i is post-incremented. i wraps around (this behavior is mandatory according to the C standard, and I assume it is the same in Go). i is now zero.
Next test of loop condition: i is still less than or equal to 255... so around we go.
What was the desired/expected behavior? I mean, I don't see anything that really should be some kind of language WTF or shocking behavior for a low-level language. It's just a common error, failing to take unsigned overflow into account, and/or not realizing that unsigned values never roll over to negative.
3 comments
[ 2.9 ms ] story [ 16.0 ms ] threadFirst loop iteration: i is zero. i is post-incremented and becomes 1.
Loop iterations where i is in the range 1..253 blah, blah, blah... we just keep printing and incrementing.
Loop iteration i = 254: i is post-incremented. i is now 255. This is the highest possible value for an 8-bit integer, when interpreted as an unsigned value.
Loop iteration i = 255. The check still passes. The body still executes. i is post-incremented. i wraps around (this behavior is mandatory according to the C standard, and I assume it is the same in Go). i is now zero.
Next test of loop condition: i is still less than or equal to 255... so around we go.
What was the desired/expected behavior? I mean, I don't see anything that really should be some kind of language WTF or shocking behavior for a low-level language. It's just a common error, failing to take unsigned overflow into account, and/or not realizing that unsigned values never roll over to negative.