Home >Backend Development >Golang >Causes and solutions to garbled code in golang web development

Causes and solutions to garbled code in golang web development

尚
Original
2019-12-12 09:44:483392browse

Causes and solutions to garbled code in golang web development

Golang's default encoding is utf-8, but there are various formats in web pages such as common "gb2312", "gbk", etc. When processing these encoded web pages, big garbled characters will appear. Golang does not have its own encoding and decoding package, so it needs to be processed with the help of a third-party package. Recommended: golang tutorial

Third-party package

code.google.com/p/mahonia

Usage introduction

code looks like It is hosted on Google's server. Domestic users may have to circumvent the firewall. I forked a mahonia on github.

First determine whether the encoding format you want is supported. If not, the return value is nil

func GetCharset(name string) *Charset

if mahonia.GetCharset(char) == nil {

    return nil, fmt.Errorf("%s charset not suported \n", char)}

Create a decoder in a specific encoding format

func NewDecoder(name string) Decoder

dec := mahonia.NewDecoder(char)

Then decode directly: commonly used There are direct decoding from string and decoding from io stream

func (d Decoder) ConvertString(s string) string
func (d Decoder) NewReader(rd io.Reader) *Reader

The io flow is used here, because a toolkit similar to jquery is also used below to facilitate coherent statements

rd := dec.NewReader(resp.Body)

The above is the detailed content of Causes and solutions to garbled code in golang web development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn