dpymrcl269187540 2017-07-09 23:52
浏览 38
已采纳

Golang中的简单整数声明

I didn't consider myself to be a newbie, but I can't figure out why this very simple code snippet fails to declare my integer.

func main () {

    var totalResults int

    rFile, err := os.Open("users.csv") //3 columns
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    defer rFile.Close()

    // Creating csv reader
    reader := csv.NewReader(rFile)

    lines, err := reader.ReadAll()
    if err == io.EOF {
        fmt.Println("Error:", err)
        return
    } else {

    }

    totalResults=len(lines)

}

It always says the value is not declared, this seems too simple.

I'm pretty sure it would work if I declared it using :=, but I wanted to declare everything at the top of the function.

  • 写回答

1条回答 默认 最新

  • drhg24275 2017-07-10 11:00
    关注

    change your code:

    lines, err := reader.ReadAll()
    if err == io.EOF {
        fmt.Println("Error:", err)
        return
    } else {
    
    }
    
        totalResults=len(lines)
    
    }
    

    to:

        lines, err := reader.ReadAll()
    if err == io.EOF {
        totalResults=len(lines)
    } else {
        fmt.Println("Error:", err)
        return
    }
        fmt.Println("total results:", totalResults)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?