golang 编码转换解决方案


本文摘自网络,作者,侵删。

代码来自gf框架
https://github.com/gogf/gf/blob/master/encoding/gcharset/gcharset.go
gcharset.go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package charset implements character-set conversion functionality.
//
// Supported Character Set:
//
// Chinese : GBK/GB18030/GB2312/Big5
//
// Japanese: EUCJP/ISO2022JP/ShiftJIS
//
// Korean  : EUCKR
//
// Unicode : UTF-8/UTF-16/UTF-16BE/UTF-16LE
//
// Other   : macintosh/IBM*/Windows*/ISO-*
package gcharset
import (
    "bytes"
    "errors"
    "fmt"
    "io/ioutil"
    "golang.org/x/text/encoding"
    "golang.org/x/text/encoding/ianaindex"
    "golang.org/x/text/transform"
)
var (
    // Alias for charsets.
    charsetAlias = map[string]string{
        "HZGB2312": "HZ-GB-2312",
        "hzgb2312": "HZ-GB-2312",
        "GB2312":   "HZ-GB-2312",
        "gb2312":   "HZ-GB-2312",
    }
)
// Supported returns whether charset <charset> is supported.
func Supported(charset string) bool {
    return getEncoding(charset) != nil
}
// Convert converts <src> charset encoding from <srcCharset> to <dstCharset>,
// and returns the converted string.
// It returns <src> as <dst> if it fails converting.
func Convert(dstCharset string, srcCharset string, src string) (dst string, err error) {
    if dstCharset == srcCharset {
        return src, nil
    }
    dst = src
    // Converting <src> to UTF-8.
    if srcCharset != "UTF-8" {
        if e := getEncoding(srcCharset); e != nil {
            tmp, err := ioutil.ReadAll(
                transform.NewReader(bytes.NewReader([]byte(src)), e.NewDecoder()),
            )
            if err != nil {
                return "", fmt.Errorf("%s to utf8 failed. %v", srcCharset, err)
            }
            src = string(tmp)
        } else {
            return dst, errors.New(fmt.Sprintf("unsupport srcCharset: %s", srcCharset))
        }
    }
    // Do the converting from UTF-8 to <dstCharset>.
    if dstCharset != "UTF-8" {
        if e := getEncoding(dstCharset); e != nil {
            tmp, err := ioutil.ReadAll(
                transform.NewReader(bytes.NewReader([]byte(src)), e.NewEncoder()),
            )
            if err != nil {
                return "", fmt.Errorf("utf to %s failed. %v", dstCharset, err)
            }
            dst = string(tmp)
        } else {
            return dst, errors.New(fmt.Sprintf("unsupport dstCharset: %s", dstCharset))
        }
    } else {
        dst = src
    }
    return dst, nil
}
// ToUTF8 converts <src> charset encoding from <srcCharset> to UTF-8 ,
// and returns the converted string.
func ToUTF8(srcCharset string, src string) (dst string, err error) {
    return Convert("UTF-8", srcCharset, src)
}
// UTF8To converts <src> charset encoding from UTF-8 to <dstCharset>,
// and returns the converted string.
func UTF8To(dstCharset string, src string) (dst string, err error) {
    return Convert(dstCharset, "UTF-8", src)
}
// getEncoding returns the encoding.Encoding interface object for <charset>.
// It returns nil if <charset> is not supported.
func getEncoding(charset string) encoding.Encoding {
    if c, ok := charsetAlias[charset]; ok {
        charset = c
    }
    if e, err := ianaindex.MIB.Encoding(charset); err == nil && e != nil {
        return e
    }
    return nil
}

还有一个解决方案: https://studygolang.com/articles/2201

但是github.com/mahonia 这个库是空的。
现在应该是这个: github.com/axgle/mahonia
问题是此库在 golang 1.16下会产生阻塞,不知道为啥,还在排查中。


本文来自:简书

感谢作者:我爱张智容

查看原文:golang 编码转换解决方案

相关阅读 >>

Go入门-1 变量

手撸Golang 结构型设计模式 享元模式

range 带中文的字符串的坑

leetcode354 俄罗斯套娃信封问题 Golang

手撸Golang etcd raft协议之3

手撸Golang 基本数据结构与算法 图的最短路径 贝尔曼-福特算法

手撸Golang 基本数据结构与算法 链表

Golang sqlx捕捉错误

rtmp协议视频平台easydss编译过程中Go语言异步信息处理设计与实现

Golang指针转字符串,Golang字符串转指针

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...