Go realizes the mutual conversion between GBK and UTF-8 encoding

Install

go get github.com/axgle/mahonia

Convert between GBK and UTF-8 encoding

func TestEncoder(t *testing.T) {
    
    
	// 编码UTF-8成GBK
	enc := mahonia.NewEncoder("gbk")
	str_gbk := enc.ConvertString("hello,世界")
	fmt.Println(str_gbk)
	// 解码GBK为UTF-8
	decoder := mahonia.NewDecoder("gbk")
	str_utf8 := decoder.ConvertString(str_gbk)
	fmt.Println(str_utf8)
}

reference

Solve Chinese garbled characters

Guess you like

Origin blog.csdn.net/lilongsy/article/details/131383405