golang处理中文时默认是utf8,当遇到其他如GBK字符是就会出现乱码,此处介绍golang 官方golang.org/x/text/encoding/simplifiedchinese包下的编码转换
package mainimport "golang.org/x/text/encoding/simplifiedchinese"type Charset stringconst ( UTF8 = Charset("UTF-8") GB18030 = Charset("GB18030"))func ConvertByte2String(byte []byte, charset Charset) string { var str string switch charset { case GB18030: var decodeBytes,_=simplifiedchinese.GB18030.NewDecoder().Bytes(byte) str= string(decodeBytes) case UTF8: fallthrough default: str = string(byte) } return str}