go-iconv: Go 语言的编码转换

Showing 1-5 of 5 messages
go-iconv: Go 语言的编码转换 xushiwei 6/12/11 12:18 AM
Go 语言官方没有提供处理 utf8、unicode 之外字符编码的包。故此在 libiconv 上包装了一个:

Summary

go-iconv is a libiconv wrapper for go. libiconv Convert string to requested character encoding.

go-iconv project's homepage is: https://github.com/xushiwei/go-iconv

Install

git clone git@github.com:xushiwei/go-iconv.git
cd go-iconv
make install

Example

Convert string

import (
"fmt"
"xushiwei.com/iconv"
)

func main() {

cd, err := iconv.Open("gbk", "utf-8")
if err != nil {
fmt.Println("iconv.Open failed!")
return
}
defer cd.Close()

gbk := cd.ConvString("你好,世界!")

fmt.Println(gbk)
}

Output to io.Writer

import (
"fmt"
"xushiwei.com/iconv"
)

func main() {

cd, err := iconv.Open("gbk", "utf-8")
if err != nil {
fmt.Println("iconv.Open failed!")
return
}
defer cd.Close()

output := ... // eg. output := os.Stdout || ouput, err := os.Create(file)
autoSync := false // buffered or not
bufSize := 0 // default if zero
w := iconv.NewWriter(cd, output, bufSize, autoSync)

fmt.Fprintln(w, "你好,世界!")

w.Sync() // if autoSync = false, you need call Sync() by yourself
}

Input from io.Reader

import (
"fmt"
"io"
"os"
"xushiwei.com/iconv"
)

func main() {

cd, err := iconv.Open("utf-8", "gbk") // gbk => utf8
if err != nil {
fmt.Println("iconv.Open failed!")
return
}
defer cd.Close()

input := ... // eg. input := os.Stdin || input, err := os.Open(file)
bufSize := 0 // default if zero
r := iconv.NewReader(cd, input, bufSize)

_, err = io.Copy(os.Stdout, r)
if err != nil {
fmt.Println("\nio.Copy failed:", err)
return
}
}

Re: {Golang-China} go-iconv: Go 语言的编码转换 sha da 6/12/11 6:47 AM
非常好,记住了,有需要时down一份。

2011/6/12 shiwei xu <xushi...@gmail.com>

Re: go-iconv: Go 语言的编码转换 jobs huang 8/23/12 5:11 AM
包安装出错

C:\Go\src\pkg\github.com\go-iconv\src>make install
Makefile:1: C:\Go\/src/Make.inc: No such file or directory
Makefile:12: C:\Go\/src/Make.pkg: No such file or directory
make: *** No rule to make target `C:\Go\/src/Make.pkg'.  Stop.


xushiwei於 2011年6月12日星期日UTC+8下午3時18分00秒寫道:
Re: [gocn:5125] Re: go-iconv: Go 语言的编码转换 Ruiqi Hong 8/23/12 5:21 AM
这个库没有更新到go1,有人fork更新了

go get github.com/grafov/go-iconv

2012/8/23 jobs huang <jobs....@gmail.com>:
> --
> 官网: http://golang-china.org/
> IRC: irc.freenode.net #golang-china
> @golangchina
Re: [gocn:5125] Re: go-iconv: Go 语言的编码转换 xushiwei 8/23/12 6:47 PM
嗯,我回头 merge 下这个 fork 的内容。

2012/8/23 Ruiqi Hong <hong...@gmail.com>



--
许式伟