I am using go language and I want to iterate a map with its keys and values all over a map, at the same time, I also want to count the number of items in the map
I tried this:
for i := 0; k,v := range map; i++ { }
I just want to know if for ...
range statement can work with i++
which is usual part of for
statement
The range clause of the for statement doesn't allow this. You have to write, for example:
var i int
for k, v := range myMap {
whatever()
i++
}
Note that if you don't mutate the map while iterating over it then
i == len(myMap)
is true afterwards.
As you must have discovered when you tried it, that doesn't work. You have to just spell it out:
i := 0
for k, v := range someMap {
//...
i++
}