How to properly output a string in a Windows console with go?
character-encoding, go, utf-8, windows
Solution
Since 2016, You can now (2017) consider the `golang.org/x/text`, which comes with a encoding charmap including the ISO-8859 family as well as the Windows 1252 character set.
See "Go Quickly - Converting Character Encodings In Golang"
r := charmap.ISO8859_1.NewDecoder().Reader(f)
io.Copy(out, r)
That is an extract of an example opening a ISO-8859-1 source text (`my_isotext.txt`), creating a destination file (`my_utf.txt`), and copying the first to the second. But to decode from ISO-8859-1 to UTF-8, we wrap the original file reader (`f`) with a decoder.
I just tested (pseudo-code for illustration):
package main
import (
"fmt"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
)
func main() {
t := "string composed of character in cp 850"
d := charmap.CodePage850.NewDecoder()
st, err := d.String(t)
if err != nil {
panic(err)
}
fmt.Println(st)
}
The result is a string readable in a Windows CMD. See more in this Nov. 2018 reddit thread.
Problem
I have a `exe` in go which prints utf-8 encoded strings, with special characters in it. Since that exe is made to be used from a console window, its output is mangled because Windows uses `ibm850` encoding (aka `code page 850`). How would you make sure the go `exe` print correctly encoded strings for a console windows, ie print for instance: ``` éèïöîôùòèìë ``` instead of (without any translation to the right charset) ``` ├®├¿├»├Â├«├┤├╣├▓├¿├¼├½ ```