2014-07-03 14:02
浏览 136


I'm developing a project where we need to persist our information in a legacy database that has ISO-8859-1 tables. So before writing something to the database I need to convert it from UTF-8 to ISO-8859-1, and every time I retrieve it from the database, I need to convert it back to UTF-8.

I was trying to use the library as the following for each text field that I need to persist.

import (
  _ ""

func toISO88591(utf8 string) string {
    buf := new(bytes.Buffer)

    w, err := charset.NewWriter("latin1", buf)
    if err != nil {
    defer w.Close()

    fmt.Fprintf(w, utf8)
    return buf.String()

func fromISO88591(iso88591 string) string {
    r, err := charset.NewReader("latin1", strings.NewReader(iso88591))
    if err != nil {

    buf, err := ioutil.ReadAll(r)
    if err != nil {

    return string(buf)

The problem is that the data is still persisted in UTF-8 even if I use the function toISO88591. I am doing something wrong in this conversion?

My database is a MySQL, and I'm using the driver with the following connection parameters:


Best regards!

1条回答 默认 最新

相关推荐 更多相似问题