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

时间:2017-05-05 18:13:51

标签: function go

我尝试运行此代码时收到此错误: 语法错误:意外的cornerFinder,期待(

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)

它在代码的第二行给出了这个错误:

func cornerFinder(censusData [] CensusGroup){

我认为我缺少一些微不足道的东西。被卡住了几个小时。帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

Function declarations仅允许top level。改为将function literal分配给本地变量。

    var cornerFinder func(censusData []CensusGroup)
    cornerFinder = func(censusData []CensusGroup) {
       ... function body from the question
    }
    cornerFinder(censusData)

此处未使用short variable declaration,因为该函数以递归方式调用自身。