Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qiniu/iconv
Golang bindings to libiconv - Convert string to requested character encoding
https://github.com/qiniu/iconv
Last synced: about 1 month ago
JSON representation
Golang bindings to libiconv - Convert string to requested character encoding
- Host: GitHub
- URL: https://github.com/qiniu/iconv
- Owner: qiniu
- License: apache-2.0
- Created: 2013-03-10T13:40:53.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2020-06-26T16:07:53.000Z (about 4 years ago)
- Last Synced: 2024-06-18T12:44:29.252Z (3 months ago)
- Language: Go
- Size: 40 KB
- Stars: 475
- Watchers: 28
- Forks: 110
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
iconv: libiconv for go
======[![LICENSE](https://img.shields.io/github/license/qiniu/iconv.svg)](https://github.com/qiniu/iconv/blob/master/LICENSE)
[![Build Status](https://travis-ci.org/qiniu/iconv.svg?branch=master)](https://travis-ci.org/qiniu/iconv)
[![Go Report Card](https://goreportcard.com/badge/github.com/qiniu/iconv)](https://goreportcard.com/report/github.com/qiniu/iconv)
[![GitHub release](https://img.shields.io/github/v/tag/qiniu/iconv.svg?label=release)](https://github.com/qiniu/iconv/releases)
[![Coverage Status](https://codecov.io/gh/qiniu/iconv/branch/master/graph/badge.svg)](https://codecov.io/gh/qiniu/iconv)
[![GoDoc](https://img.shields.io/badge/Godoc-reference-blue.svg)](https://godoc.org/github.com/qiniu/iconv)[![Qiniu Logo](http://open.qiniudn.com/logo.png)](http://www.qiniu.com/)
iconv is a libiconv wrapper for go. libiconv Convert string to requested character encoding.
# Document
See http://godoc.org/github.com/qiniu/iconv
Note: Open returns a conversion descriptor cd, cd contains a conversion state and can not be used in multiple threads simultaneously.
# Install
```
go get github.com/qiniu/iconv
```# Example
## Convert string
```go
package mainimport (
"fmt"
"github.com/qiniu/iconv"
)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
```go
package mainimport (
"fmt"
"github.com/qiniu/iconv"
)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
```go
package mainimport (
"fmt"
"io"
"os"
"github.com/qiniu/iconv"
)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
}
}
```