Handle a specific error in golang


I am trying to simply ignore the csv.ErrFieldCount error in our code but cannot seem to only look at that one error. What am I doing wrong here?

record, err := reader.Read()
if err != nil {
    if err == csv.ErrFieldCount {
        return nil
    return err

But when I run the code the last line of the csv file gives me this error paniced line 11535, column 0: wrong number of fields in line


csv.Reader doesn’t return that error, it returns a csv.ParseError. You first need to check if you have a ParseError, then check the Err field:

if err, ok := err.(*csv.ParseError); ok && err.Err == csv.ErrFieldCount {
    return nil

Answered By – JimB

Answer Checked By – Jay B. (GoLangFix Admin)

Leave a Reply

Your email address will not be published.