I have been writing a program to parse a csv file using the encoding/csv package. For some reason I have begul getting sporadic EOF errors. It is very strange since I am using the same file to test with over and over. Sometimes it parses fine. Other times it exits with an EOF.
This is a simplified version of the code:
main.go
package main
import (
"bytes"
"encoding/csv"
"fmt"
"os"
"path/filepath"
"unicode/utf8"
)
func main() {
LoadConfig()
csvfile, err := os.Open(config.MonitorDir + "/test.csv")
if err != nil {
fmt.Println(err)
os.Exit(2)
}
reader := csv.NewReader(csvfile)
defer func() {
csvfile.Close()
MoveFile(csvfile)
}()
reader.Comma, _ = utf8.DecodeRuneInString(config.Delimiter)
reader.LazyQuotes = true
writeHeaders(reader)
}
func writeHeaders(reader *csv.Reader) {
Headers, err := reader.Read() // This is where I see the EOF
// (Headers = [] err = EOF)
if err != nil {
fmt.Printf("Error is: %s
", err.Error())
os.Exit(2)
}
fmt.Println(Headers)
}
func MoveFile(file *os.File) {
err := os.MkdirAll(Concat(config.MonitorDir, "/completed/"), os.ModeDir)
if err != nil {
fmt.Printf("Error is: %s
", err.Error())
os.Exit(2)
}
err = os.Rename(file.Name(), Concat(config.MonitorDir, "/completed/", filepath.Base(file.Name())))
if err != nil {
return
}
}
func Concat(str ...string) string {
var buffer bytes.Buffer
for i := 0; i < len(str); i++ {
buffer.WriteString(str[i])
}
return buffer.String()
}
conf.go
package main
import (
"encoding/json"
"fmt"
"os"
)
type Configuration struct {
MonitorDir string
LogPath string
Delimiter string
}
var config Configuration
func LoadConfig() {
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
config = Configuration{}
err := decoder.Decode(&config)
if err != nil {
fmt.Printf("Error is: %s
", err.Error())
os.Exit(2)
}
}
test.csv
Id,LastModified
"108420","2015-04-25T11:34:35.437332096-06:00"
"132774","2015-04-25T11:34:35.437332096-06:00"
"254790","2015-04-25T11:34:35.437332096-06:00"
"359972","2015-04-25T11:34:35.437332096-06:00"
"363060","2015-04-25T11:34:35.437332096-06:00"
This has become a blocking issue as it might throw 8, 10 or even more EOF before finally processing the file. I presume I am doing something strange to cause it. But, I cannot for the life of me see it.