iconv

Golang bindings to libiconv - Convert string to requested character encoding

  • 所有者: mreiferson/go-ujson
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

iconv: libiconv for go

Build Status Build Status

logo

iconv is a libiconv wrapper for go. libiconv Convert string to requested character encoding.
iconv project's homepage is: https://github.com/go-iconv/iconv.
Fork from : https://github.com/qiniu/iconv.

why go-iconv?

support gopkg.in API

Document

See http://godoc.org/gopkg.in/iconv.v1

Note: Open returns a conversion descriptor cd, cd contains a conversion state and can not be used in multiple threads simultaneously.

Install

go get gopkg.in/iconv.v1

Example

Convert string

package main

import (
	"fmt"
	"gopkg.in/iconv.v1"
)

func main() {

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

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

	fmt.Println(gbk)
}

Output to io.Writer

package main

import (
	"fmt"
	"gopkg.in/iconv.v1"
)

func main() {

	cd, err := iconv.Open("gbk", "utf-8") // convert utf-8 to gbk
	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

package main

import (
	"fmt"
	"io"
	"os"
	"gopkg.in/iconv.v1"
)

func main() {

	cd, err := iconv.Open("utf-8", "gbk") // convert gbk to 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
	}
}

主要指標

概覽
名稱與所有者mreiferson/go-ujson
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2013-03-08 23:57:54
推送於2020-06-14 02:14:15
最后一次提交2020-06-13 19:14:06
發布數0
用户参与
星數73
關注者數8
派生數16
提交數18
已啟用問題?
問題數3
打開的問題數2
拉請求數3
打開的拉請求數0
關閉的拉請求數5
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?