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

Causes and solutions to garbled code in golang web development

Release: 2019-12-12 09:44:48
Original
3369 people have browsed it

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)}
Copy after login

Create a decoder in a specific encoding format

func NewDecoder(name string) Decoder

dec := mahonia.NewDecoder(char)
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template