Reputation: 29
Is there an explanation why for loop using uint64 wouldn't stop at 0?
I've tried the same for loop with int and it works as expected.
package main
import (
"fmt"
)
func main() {
i := uint64(5)
for ; i>=uint64(0); i-- {
fmt.Printf("step %d\n", i)
}
}
I would expect this function to stop at output step 0
but it overflows the int and continues forever with step 18446744073709551615
and so on
Upvotes: 0
Views: 132
Reputation: 50017
Your loop continues while i >= 0
, but an unsigned integer is always greater than or equal to zero. Unsigned integers cannot be negative so your loop never terminates.
Try changing i
to int64
and see if it behaves differently. :-)
Best of luck.
Upvotes: 2