How to write csv file with bom-utf8?

I’m useing encoding/csv to create csv file on my mac , but when I send it to a people who use windows, the content is garbled, I searched the reason and found it need write a bom-utf8 head, but it didn’t work

here is my code:

import (

func main() {
	file, err := os.Create("result.csv")
	if err != nil {
		log.Println(`create file error:`, err)
	defer file.Close()
	writer := csv.NewWriter(file)
	bomUtf8 := []byte{0xEF, 0xBB, 0xBF}
1 Like

You have to write the BOM directly to your file: file.Write(bomUtf8), then use the writer for your CSV data.

Remember to check the errors from both file.Write and writer.Write (and Flush)!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.