Jose Villalta
Jose Villalta

Reputation: 1479

Parsing csv file in Go, extra bytes in first row first column

I'm new to Go and I'm trying to get the column names by parsing the first row of the csv file. When I compare the value of the string against the expected string value the comparison says they are not equal and I can't figure out why. When I print the byte values I notice there are 3 extra bytes in the beginning of the parsed string. This only seems to happen to the first row of the first column which tells me it has something to do with the file format? I'm not sure, I didn't see anything in the CSV go reference. I apologize in advance if this is a "dumb" question.

test.csv :

name, zip code, foo

John, 91201, blah

Mary, 98108, meh Bob, 12345, boo

package main

import (
    "encoding/csv"
    "fmt"
    "os"
)

func main() {
    var file, err = os.Open("test.csv")
    if err != nil{
        fmt.Errorf("Error opening File")
    }
    reader := csv.NewReader(file)
    record, err := reader.Read()
    if err != nil{

    }
    val := record[0]

    for i := 0; i<len(val); i++{
        fmt.Printf("%x ", val[i])
    }
    name := "name"
    fmt.Println(" ")
    for i := 0; i<len(name); i++{
        fmt.Printf("%x ", name[i])
    }


    if val != "name"{
        fmt.Println("Did not match name")
    } else {
        fmt.Println("found it!")
    }

}

The output looks like this:

ef bb bf 6e 61 6d 65  
6e 61 6d 65 
Did not match name

Where does the "ef bb bf" come from?

Upvotes: 1

Views: 1544

Answers (1)

Jose Villalta
Jose Villalta

Reputation: 1479

A coworker mentioned that it might be a Byte Order Mark (BOM)

https://softwareengineering.stackexchange.com/questions/372692/should-utf-8-csv-files-contain-a-bom-byte-order-mark

Upvotes: 5

Related Questions