How to properly output a string in a Windows console with go?

前端 未结 4 1103
难免孤独
难免孤独 2021-01-04 13:32

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

4条回答
  •  抹茶落季
    2021-01-04 13:54

    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.

提交回复
热议问题