恐慌:行尾有多余的定界符

I'm reading the MaxMind GeoIP Lite City locations CSV file using Go:

csvFile, err := os.Open("/path/GeoLiteCity_20130702/GeoLiteCity-Location.csv")
defer csvFile.Close()

if err != nil {
    panic(err)
}

csvf := csv.NewReader(csvFile)
csvf.Read()     // skip header row


for {
    fields, err := csvf.Read()

    if err == io.EOF {
        break
    } else if err != nil {
        panic(err)
    }

    // does nothing yet
}

The error I'm getting is:

panic: line 2, column 22: extra delimiter at end of line

goroutine 1 [running]: main.main() /path/myprogram.go:239 +0x108f

goroutine 2 [runnable]: exit status 2

The file is quite long, but starts with these lines:

locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
1,O1,,,,0.0000,0.0000,,
2,AP,,,,35.0000,105.0000,,
3,EU,,,,47.0000,8.0000,,
4,AD,,,,42.5000,1.5000,,
5,AE,,,,24.0000,54.0000,,
6,AF,,,,33.0000,65.0000,,
7,AG,,,,17.0500,-61.8000,,
8,AI,,,,18.2500,-63.1667,,
9,AL,,,,41.0000,20.0000,,

It appears to be properly formatted. Each row has 9 fields.

Line 239 is my line invoking the panic, panic(err). As you can see, it's failing on line 2 of the CSV file, which happens in the first iteration of the loop (line 1 is read before the loop, to skip the header row). Column 22 of line 2 is the second-to-last comma.

Am I missing something here? I don't see any trailing comma... (clarification: the commas at the end of each line must be there to indicate empty field values, so they're not trailing, as in, extra.)

UPDATE: The gophers have resolved this issue and the fix ships with Go 1.1.2.

There are even two trailing commas on each line.

Try setting csv.Reader.TrailingComma = true.

It really often helps taking a look at the source or at least the package documentation :-)

Here is a complete example for you. The key is csvf.TrailingComma = true.

package main

import (
    "bytes"
    "encoding/csv"
    "fmt"
    "io"
)

var csvData = `locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
1,O1,,,,0.0000,0.0000,,
2,AP,,,,35.0000,105.0000,,
3,EU,,,,47.0000,8.0000,,
4,AD,,,,42.5000,1.5000,,
5,AE,,,,24.0000,54.0000,,
6,AF,,,,33.0000,65.0000,,
7,AG,,,,17.0500,-61.8000,,
8,AI,,,,18.2500,-63.1667,,
9,AL,,,,41.0000,20.0000,,
`

func main() {
    csvFile := bytes.NewBufferString(csvData)
    csvf := csv.NewReader(csvFile)
    csvf.TrailingComma = true
    csvf.Read() // skip header row

    for {
        fields, err := csvf.Read()

        if err == io.EOF {
            break
        } else if err != nil {
            panic(err)
        }

        // does nothing yet
        fmt.Println(fields)
    }
}