I try to read a non-utf8 encoded file and print out the content. like:
content, _ := os.readfile("example.csv") fmt.println(string(content))
Output:
����������������������������
I then try to convert the contents of the rune and decode it to utf8 like this:
br := make([]rune, 0) for len(content) > 0 { r, size := utf8.DecodeRune(content) br = append(br, r) content = content[size:] } fmt.Println(string(br))
But the result is the same. How can I get the correct content? ps: I don't know the file encoding type, they can be several types such as traditionalchinese.big5 or japanese.shiftjis, and the content cannot be a file. It can be a string.
Most likely you need the package text/encoding from golang.org/x/
hierarchy structure.
In particular golang.org/x/text/encoding /charmap
allows the creation of encodings. Decoder
can convert byte streams in traditional non-UTF-8 encoding into Go's native UTF-8 encoded data stream.
The above is the detailed content of Read non-UTF8 encoded file contents and print them correctly. For more information, please follow other related articles on the PHP Chinese website!