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.
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)
}