golang 编码转化
在网上搜索golang编码转化时,我们经常看到的文章是使用下面一些第三方库:
https://github.com/djimenez/iconv-go
https://github.com/qiniu/iconv
如果我们在windows下使用这个库,会看到错误:
iconv.go:6:20: fatal error: iconv.h: No such file or directory
compilation terminated.
这是因为需要系统有 iconv.h 文件。 linux、mac下自带了这个,windows 下没有。
如何让win下有这个C的代码,网上一堆说法。
比如,
1、使用 cygwin
https://github.com/qiniu/iconv/issues/6
在cygwin中安装gcc编译器
http://qichunren.iteye.com/blog/214527
反正这个环境,我在win下没有搭建起来,网上能看到这么说的:
go is not compatible with cygwin (either 32bit or 64bit), please use mingw.
https://code.google.com/p/go/issues/detail?id=7265
2、有人推荐使用 tdm gcc mingw
http://zhidao.baidu.com/question/744915659430101412.html
后来 install tdm gcc mingw to selove bellow problem 解决问题.
http://tdm-gcc.tdragon.net/download
这套方案我也没有搞定。
3、至于使用 mingw 的方案, 也没搞定。
最后搞定的方式,是发现有个直接用Go实现编码转化的包:
对应的代码如下:
import (
"bytes"
"code.google.com/p/go.text/encoding/simplifiedchinese"
"code.google.com/p/go.text/transform"
"io/ioutil"
)
func Decode(s []byte) ([]byte, error) {
I := bytes.NewReader(s)
defer I.Close()
O := transform.NewReader(I, simplifiedchinese.GBK.NewDecoder())
defer O.Close()
d, e := ioutil.ReadAll(O)
if e != nil {
return nil, e
}
return d, nil
}
需要注意的是,上面代码用的包在 code.google.com/p/go.text , 这些包都被迁移到 golang.org/x 这里了, 对应的迁移映射关系如下:
code.google.com/p/go.benchmarks -> golang.org/x/benchmarks
code.google.com/p/go.blog -> golang.org/x/blog
code.google.com/p/go.crypto -> golang.org/x/crypto
code.google.com/p/go.exp -> golang.org/x/exp
code.google.com/p/go.image -> golang.org/x/image
code.google.com/p/go.mobile -> golang.org/x/mobile
code.google.com/p/go.net -> golang.org/x/net
code.google.com/p/go.sys -> golang.org/x/sys
code.google.com/p/go.talks -> golang.org/x/talks
code.google.com/p/go.text -> golang.org/x/text
code.google.com/p/go.tools -> golang.org/x/tools
相关参考资料:
Golang 字符编码
http://www.cnblogs.com/lyqf365/p/3739533.html
这里有下载网页并转码的例子。
Go的官方编码转换包
http://blog.raphaelzhang.com/2014/01/go-official-support-for-charset-encoding/
Go如何处理zip中的中文文件名
http://my.oschina.net/chai2010/blog/186211
http://bbs.carlaau.com/go/t73-1-1.html
go language how to convert ansi text to utf8?
http://stackoverflow.com/questions/6927611/go-language-how-to-convert-ansi-text-to-utf8/6933412#6933412
另外,还有一个 go-charset 包(https://code.google.com/p/go-charset/)
相关文档在:
https://godoc.org/code.google.com/p/go-charset/charset
它支持下面这些编码的转换。
big5 ibm437 ibm850 ibm866 iso-8859-1 iso-8859-10 iso-8859-15 iso-8859-2 iso-8859-3 iso-8859-4 iso-8859-5 iso-8859-6 iso-8859-7 iso-8859-8 iso-8859-9 koi8-r utf-16 utf-16be utf-16le utf-8 windows-1250 windows-1251 windows-1252
它的相关例子请参考:http://stackoverflow.com/questions/24555819/golang-persist-using-iso-8859-1-charset
package main
import (
"bytes"
"code.google.com/p/go-charset/charset"
_ "code.google.com/p/go-charset/data"
"fmt"
"io/ioutil"
"strings"
)
func toISO88591(utf8 string) (string, error) {
buf := new(bytes.Buffer)
w, err := charset.NewWriter("latin1", buf)
if err != nil {
return "", err
}
fmt.Fprintf(w, utf8)
w.Close()
return buf.String(), nil
}
func fromISO88591(iso88591 string) (string, error) {
r, err := charset.NewReader("latin1", strings.NewReader(iso88591))
if err != nil {
return "", err
}
buf, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}
return string(buf), nil
}
func main() {
utfi := "£5 for Peppé"
fmt.Printf("%q\n", utfi)
iso, err := toISO88591(utfi)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%q\n", iso)
utfo, err := fromISO88591(iso)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%q\n", utfo)
fmt.Println(utfi == utfo)
}
上面代码的输出:
"£5 for Peppé"
"\xa35 for Pepp\xe9"
"£5 for Peppé"
true
golang 编码转化的更多相关文章
- golang编码转换
在网上搜索golang编码转化时,我们经常看到的文章是使用下面一些第三方库: https://github.com/djimenez/iconv-go https://github.com/qiniu ...
- IOS编码转化
原文地址:http://blog.csdn.net/huifeidexin_1/article/details/7883984 iOS中编码转化 1.UTF-8转化 NSString *data = ...
- Nodejs编码转化问题
目前Node.js仅支持hex.utf8.ascii.binary.base64.ucs2几种编码的转换.对于GBK,GB2312等编码,Nodejs自带的toString()方法不支持,因此中文转化 ...
- 宽字符、多字节、unicode、utf-8、gbk编码转化
今天遇到一个编码的问题,困惑了我很长时间,所以就简要的的了解了一下常用的编码类型. 我们最常见的是assic编码,它是一种单字节编码,对多容纳256个字符. 我们在编程的时候经常遇到unicode,u ...
- [ACM_模拟] POJ1068 Parencodings (两种括号编码转化 规律 模拟)
Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...
- 报表开发工具中mysql数据库连接编码转化失效解决方案
1. 问题描述 在报表开发工具FineReport中,mysql数据库连接通过数据连接编码转换进行编码的转换,在通过报表录入往数据库中录入中文数据的时候,总是出现乱码,这个该怎么解决呢? 2. 解决方 ...
- java编码转化方案-备用
import java.io.UnsupportedEncodingException; /** * 转换字符串的编码 */ public class changeCharSet { /** 7位AS ...
- 将UTF8编码转化为中文 - NSString方法
方法一: 代码如下,如有更好的方法 麻烦贴出来,这个方法是通过webview进行解码的 UIWebView *web = [[UIWebView alloc] init]; NSString *tsw ...
- native2ascii -- 编码转化工具
参考文档 http://blog.chinaunix.net/uid-692788-id-2681133.html 功能说明 Java 编译器和其它 Java 工具只能处理含有 Latin-1 和/或 ...
随机推荐
- static修饰的方法不能被重写可以被继承
今天我们谈谈为什么抽象类中不能有静态的抽象方法以及static修饰的方法不能被重写可以被继承 1 static修饰的方法不能被重写可以被继承我们知道static修饰的方法为静态方法,可以直接使用类名. ...
- 【bug】—— H5页面在 ios 端滑动不流畅的问题
IOS系统的惯性滑动效果非常6,但是当我们对div加overflow-y:auto;后是不会出这个效果的,滑动的时候会感觉很生涩.怎么办? 方案一: 在滚动容器内加-webkit-overflow-s ...
- 理解 atime,ctime,mtime (下)
话不多说,开始下篇. # 前言 通过 "理解 atime,ctime,mtime (上)" 我们已经知道了atime 是文件访问时间:ctime是文件权限改变时间:mtime是文件 ...
- Dota2APP--第一天
从今天开始,下定决心自己独立开发一个有关于Dota2的APP,因为非常喜欢这个游戏恰好自己又是做iOS移动开发的所以萌生了这个想法.希望可以坚持下去,有喜欢dota2的朋友也可以提点建议. 一.项目的 ...
- Linux之virtualbox中的ubuntu虚拟机linux系统共享文件夹
windows通过virtualbox软件与linux系统机型文件共享 1.第一步 在设置中找到共享文件夹选项,选择添加共享文件夹 2.第二步 选择需要与linux进行共享的文件夹,并选择固定分配 3 ...
- 无比迅速敏捷地开发iOS超精美控件
目录 前言 设计 编码 PaintCode 前言 自从人生第一篇博客<iOS中的预编译指令的初步探究>问世以来 浏览量竟然达到了360多,(路过的大神勿笑!)这些浏览量使我兴奋异常但又令我 ...
- Android NDK开发及OpenCV初步学习笔记
https://www.jianshu.com/p/c29bb20908da Android NDK开发及OpenCV初步学习笔记 Super_圣代 关注 2017.08.19 00:55* 字数 6 ...
- C# this关键字(给底层类库扩展成员方法)
本文参考自唔愛吃蘋果的C#原始类型扩展方法—this参数修饰符,并在其基础上做了一些细节上的解释 1.this作为参数关键字的作用 使用this关键字,可以向this关键字后面的类型添加扩展方法,而无 ...
- Springboot用官方建议访问Html页面并接传值
Springboot用官方建议访问Html页面并接传值 我们以前通常习惯用webapp来防止jsp页面,但是到了Springboot中,官方建议用Static文件夹来存放及静态的资源, 用templa ...
- 静态代理,动态代理,Cglib代理详解
一.静态代理 新建一个接口 定义一个玩家方法: package com."".proxy.staticc; public interface Iplayer { public vo ...