错误的函数声明语法错误:意外的cornerFinder,期望(

huangapple go评论97阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2017年5月6日 02:13:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/43811387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定