0

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?

jhaubrich.com
  • 79
  • 2
  • 10
  • 6
    It is Golang not a TypeScript. – RAZAFINARIVO Hanania Oct 28 '21 at 16:33
  • [What is the difference between := and = in Go?](https://stackoverflow.com/questions/36512919/what-is-the-difference-between-and-in-go), [Range Clauses (go Wiki)](https://github.com/golang/go/wiki/Range), [Difference between for loop construct vs range keyword in go](https://stackoverflow.com/questions/30955792/difference-between-for-loop-construct-vs-range-keyword-in-go) – D M Oct 28 '21 at 16:34

1 Answers1

0

As RAZAFINARIVO has pointed out in the comments of my question, the function is actually from the Golang language, not Typescript. I was mistaken in thinking it was Typescript. Thanks RAZAFINARIVO!

jhaubrich.com
  • 79
  • 2
  • 10