Skip to content

map loop count error #73708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
runsys opened this issue May 14, 2025 · 1 comment
Closed

map loop count error #73708

runsys opened this issue May 14, 2025 · 1 comment
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@runsys
Copy link

runsys commented May 14, 2025

package main import ( "encoding/json" "fmt" "io/ioutil" "log" "os" "strings" "time" ) func Is(v I) string { return strconv.FormatInt(int64(v), 10) } func main() { rm := make(map[string]bool, 0) i := 1 rm["0"] = true lcnt := 0 for u, _ := range rm { if i > 1 && u == "0" { panic("error u 0") } j := 0 for ; i < 100000000; i += 1 { j += 1 rm[Is(i)] = true if j > 1000 { break } } lcnt += 1 } fmt.Println("loop count", lcnt) return } result: loop count 6 result: loop count 5

https://walib.run.place:5445/sharedown/02F428E11BF301DA/share/go%20loop%20count%20error.txt

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label May 14, 2025
@ALTree
Copy link
Member

ALTree commented May 14, 2025

You're getting random counts because you're adding to the map while iterating over it, which is documented as having unpredictable results.

Closing here as it's not a bug.

@ALTree ALTree closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

3 participants