英文:
incorrect function declaration syntax error: unexpected cornerFinder, expecting (
问题
当我尝试运行这段代码时,我收到以下错误信息:
语法错误:意外的cornerFinder,期望(
代码的第二行出现了这个错误:
func cornerFinder(censusData []CensusGroup) {
我认为这是一个微不足道的错误,我可能忽略了什么。我已经卡在这上面几个小时了。希望能得到帮助。
英文:
I receive this error when I try to run this code:
syntax error: unexpected cornerFinder, expecting (
case "-v2":
func cornerFinder(censusData []CensusGroup) {
if len(censusData) <= 10000{
for i := 0; i <= 10000; i++ {
if (censusData.latitude > maxLat){
maxLat = censusData.latitude
}
if (censusData.latitude < minLat){
minLat = censusData.latitude
}
if (censusData.longitude > maxLong){
maxLong = censusData.longitude
}
if (censusData.longitude < minLong){
minLong = censusData.longitude
}
}
}
mid := len(data)/2
done := make(chan bool)
go func() {
cornerFinder(censusData[:mid])
done<- true
} ()
cornerFinder(censusData[mid:len(censusData)])
<-done
return
}
cornerFinder(censusData)
Its giving this error on the second line of the code:
func cornerFinder(censusData []CensusGroup) {
I think it something trivial that I am missing. Been stuck on it for a couple of hours. Help would be appreciated
答案1
得分: 3
函数声明只允许在顶层进行。可以将函数字面量赋值给一个局部变量。
var cornerFinder func(censusData []CensusGroup)
cornerFinder = func(censusData []CensusGroup) {
... 问题中的函数体
}
cornerFinder(censusData)
这里没有使用短变量声明,因为函数在递归调用自身。
英文:
Function declarations are only allowed at top level. Assign a function literal to a local variable instead.
var cornerFinder func(censusData []CensusGroup)
cornerFinder = func(censusData []CensusGroup) {
... function body from the question
}
cornerFinder(censusData)
A short variable declaration is not used here because the function calls itself recursively.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论