Golang method to solve the Chinese garbled

Solution:

Introducing a third transcoded packet sent:

git clone https://github.com/axgle/mahonia.git

Example:

. 1  Package main
 2  Import (
 . 3     "BUFIO"
 . 4     "FMT"
 . 5     "IO"
 . 6     "Mahonia" // transcoding
 . 7     "OS"
 . 8  )
 . 9  
10  FUNC main () {
 . 11     var ENC mahonia.Decoder
 12 is     ENC = Mahonia. newDecoder ( "GBK" )
 13     // case to read the file
 14     // read file contents and displayed on the terminal, use os.Open, File.close, bufio.NewReader (), reader.ReadString
 15     file, ERR: os.Open = ( "E: /test.txt" )
 16     IF ERR!= nil {
17       fmt.Println ( "ERR = Open File" , ERR)
 18     }
 19  
20     // When the function exits, to close the File
 21     the defer File.close () // prevent memory leaks
 22     // create a * Reader, is with buffer, the buffer is 4096 bytes default
 23 is     Reader: = bufio.NewReader (file)
 24     // content loop to read the file
 25     for {
 26 is        STR, ERR: = reader.ReadString ( '\ n-') // read a newline ends
 27        IF ERR == io.EOF {// io.EOF the end of file
 28           BREAK
 29        }
 30        // output content
 31       fmt.Println ( "UTF-to GBK. 8:" , enc.ConvertString (STR))
 32     }
 33 is     fmt.Println ( "end of file read" )
 34 }

Guess you like

Origin www.cnblogs.com/mo3408/p/12216695.html