package main
import "fmt"
func main() {
mapVal := map[string]string{
"a": "k",
}
a := []string{"a", "b"}
for i := range a {
for _, ok := mapVal[a[i]]; ok; {
i++
fmt.Printf("the value of i is %d", i)
}
}
}
I did loop over the map which is a nested loop _, ok := mapVal[a[i]]; ok;
. For the first iteration i equals to 0 and a[0] == "a" which returns the value(_) and ok(bool) as true but for the second iteration value i is incremented and i equals to 1 and a[1] == "b" which means the ok(bool) should be false but it is returned as true and the loop goes for infinite.
Issue is with your inner loop. It does not reassign the value of ok for next time and if it evaluates true once, it goes into infinite loop. Here is my updated:
package main
import "fmt"
func main() {
mapVal := map[string]string{
"a": "k",
}
a := []string{"a", "b"}
for i := range a {
for _, ok := mapVal[a[i]]; ok; _, ok = mapVal[a[i]] {
i++
fmt.Printf("the value of i is %d\n", i)
}
}
}