1

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?

1

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

It is. Putting a BOM on a UTF-8 file is absolutely incorrect behavior, but some software does it anyway.
Without the BOM utf-8 csv looks like garbage in Excel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.