I am trying to write to a file a string that contains chinese characters but I have an encoding issue (getting something like that 10 年以上经验)
Here is what I have.
csvContent, err := gocsv.MarshalString(&csvTranslation) // Get all clients as CSV string
if err != nil {
panic(err)
}
d1 := []byte(string(csvContent))
ioutil.WriteFile("result.csv", d1, 0644)
Could you point me to what I am missing
You have a Go UTF-8 encoded string
and you are incorrectly interpreting it as extended ASCII bytes.
For example,
package main
import (
"fmt"
)
func main() {
utf8 := "汉字 漢字"
fmt.Println(utf8)
var ascii string
for i := 0; i < len(utf8); i++ {
ascii += string(utf8[i])
}
fmt.Println(ascii)
}
Playground: https://play.golang.org/p/HTPWhPO7OVS
Output:
汉字 漢字
æ±å æ¼¢å