I was looking at a TypeScript function to calculate an average run time and I encountered some strange syntax I haven't seen before:
func averageRuntimeInSeconds(runs []Run) float64 {
var totalTime int
var failedRuns int
for _, run := range runs {
if run.Failed {
failedRuns++
} else {
totalTime += run.Time
}
}
averageRuntime := float64(totalTime) / float64(len(runs) - failedRuns) / 1000
return averageRuntime
}
What does the
:=
symbol mean on the 4th line? Also on the 4th line of that code, the syntax for the for loop looks very strange to me. There are no parentheses. What is going on there? What kind of for loop is that?
And lastly, what does the range keyword do?