golang 編碼轉化

在網上搜索golang編碼轉化時,咱們常常看到的文章是使用下面一些第三方庫:html

https://github.com/djimenez/iconv-golinux

https://github.com/qiniu/iconvgit

若是咱們在windows下使用這個庫,會看到錯誤:github

iconv.go:6:20: fatal error: iconv.h: No such file or directory
compilation terminated.golang

這是由於須要系統有 iconv.h 文件。 linux、mac下自帶了這個,windows 下沒有。windows

如何讓win下有這個C的代碼,網上一堆說法。google

好比,編碼

一、使用 cygwin .net

https://github.com/qiniu/iconv/issues/6 code

在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

 

二、有人推薦使用 tdm gcc mingw

http://zhidao.baidu.com/question/744915659430101412.html

後來 install tdm gcc mingw to selove bellow problem 解決問題.
http://tdm-gcc.tdragon.net/download

這套方案我也沒有搞定。

 

三、至於使用 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
相關文章
相關標籤/搜索