在网上搜索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编码转换的更多相关文章

  1. golang 编码转化

    在网上搜索golang编码转化时,我们经常看到的文章是使用下面一些第三方库: https://github.com/djimenez/iconv-go https://github.com/qiniu ...

  2. windows下go编码转换问题

    github上有两个package做编码转换,都是基于iconv,用到了cgo,在linux下没有问题,在windows下用,非常麻烦.采用mingw安装libiconv也不行,一直提示找不到libi ...

  3. 使用vbs脚本进行批量编码转换

    使用vbs脚本进行批量编码转换 最近需要使用SourceInsight查看分析在Linux系统下开发的项目代码,我们知道Linux系统中文本文件默认编码格式是UTF-8,而Windows中文系统中的默 ...

  4. 关于JS的编码转换问题

    在进行JS开发过程中,尤其是在开发报表时,报表已集成到Web页面中,通过在页面传递参数至报表中时,会发现有时某些参数值,传递到报表中是显示为问号或乱码等等一系列不能正常显示的情况. 这是由于浏览器和报 ...

  5. 帆软报表FineReport数据库连接编码转换

    1. 问题描述 数据库会以某种编码方式保存与读取数据,FineReport解析时默认使用GBK字符集,若数据库端编码与设计器端编码不一致时,就会导致中文及特殊字符的乱码. FineReport在定义数 ...

  6. 编码转换的处理 DreamWeaver SC6 打开会出现javacsript出现问题的处理

      编码转换的处理: 打开DW后,修改里面有个"页面属性": 点击页面属性,会弹出一个窗口,点击"标题/编码",在"编码"里面选择你要转换的 ...

  7. C++ 字符编码转换类

    记录一下C++ 编码转换的函数: #pragma once #include "afx.h" #define DEFAULT_CODE 0 #define CHINESE_SIMP ...

  8. [2015.02.02]文本编码转换专家 v2.6

    软件名称:文本编码转换专家最新版本:v2.6操作系统:XP/2003/Win7/Win2008软件介绍:文本编码转换专家,界面简洁易用,功能强大实用.自动识别文件编码,有效转换成目标编码.真正的多线程 ...

  9. python 字符编码 转换

    #!/bin/env python#-*- encoding=utf8 -*-# 文件头指定utf8编码还是乱码时,使用下面方式指定# fix encoding problem import sys ...

随机推荐

  1. 三对角矩阵(Tridiagonal Matrices)的求法:Thomas Algorithm(TDMA)

    转载http://www.cnblogs.com/xpvincent/archive/2013/01/25/2877411.html 做三次样条曲线时,需要解三对角矩阵(Tridiagonal Mat ...

  2. c# 之五行地支

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. LA 4064 Magnetic Train Tracks

    题意:给定平面上$n(3\leq n \leq 1200)$个无三点共线的点,问这些点组成了多少个锐角三角形. 分析:显然任意三点可构成三角形,而锐角三角形不如直角或钝角三角形容易计数,因为后者有且仅 ...

  4. Android activity界面跳转动画

    实现activity界面跳转动画 1.在startActivity方法之后加入: overridePendingTransition(R.anim.pull_in_right, R.anim.pull ...

  5. 分组排序SQL

    SELECT * FROM (SELECT columns,ROWNUM AS RN FROM (SELECT DISTINCT columns FROM table WHERE 1=1)) T WH ...

  6. 对torch的一点感想

    torch是一个基于LuaJIT的科学计算框架,知乎上有个人回答说torch比较适合科研用途, torch与matlab的很多函数很相似

  7. zoj 3557 How Many Sets II

    How Many Sets II Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a set S = {1, 2, ..., n}, n ...

  8. windows7安装phpnow Apache非管理员权限不能操作Windows NT服务的解决方法

    科普一下:PHPnow 是什么?        Win32 下绿色免费的 Apache + PHP + MySQL 环境套件包.简易安装.快速搭建支持虚拟主机的 PHP 环境,可以安装 Discuz! ...

  9. Python3基础 ,= 一个等式给多个变量赋值

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  10. 【leetcode❤python】 438. Find All Anagrams in a String

    class Solution(object):    def findAnagrams(self, s, p):        """        :type s: s ...