英文:
Strange TypeScript Syntax
问题
我正在为你翻译以下内容:
我正在查看一个用于计算平均运行时间的TypeScript函数,遇到了一些我以前没见过的奇怪语法:
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
}
第4行的 :=
符号是什么意思?
还有,在代码的第4行,for循环的语法对我来说看起来很奇怪。没有括号。那是什么情况?那是什么类型的for循环?
最后,range 关键字是什么意思?
英文:
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?
答案1
得分: 0
如RAZAFINARIVO在我的问题评论中指出的那样,该函数实际上是来自Golang语言,而不是Typescript。我错误地认为它是Typescript。谢谢RAZAFINARIVO!
英文:
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论