Home > database >  GO generate incorrect UTF-8 chars like ö
GO generate incorrect UTF-8 chars like ö

Time:02-11

Ik building an GO application where I want to output a cvs string from a buffer out via the http server.

I'm putting it into the csv buffer:

var buffer bytes.Buffer
resp := csv.NewWriter(&buffer)
resp.Write("Schröder")

The output it via the http server:

resp.Flush()
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Write([]byte(buffer.String()))

When I then open my url a csv file is download en opened by Excel. In that excelsheet the field value is converted to "Schröder".

Any idee, i'm already a week stuk on this item?

CodePudding user response:

The problem is not in Go but in Excel. The information that the data are encoded in UTF-8 is lost when saving the file, since there is no such thing as an encoding attribute on saved files.

Thus Excel will just see the plan data and has no information about the encoding. There are several tricks to make Excel do the right guess, like placing the proper byte order mark (BOM) at start of the file. See Is it possible to force Excel recognize UTF-8 CSV files automatically?. But just specifying charset=utf-8 within the HTTP Content-type header will not help since Excel does not get this information.

  • Related