iconv is a libiconv wrapper for go. libiconv Convert string to requested character encoding.
iconv project's homepage is: https://github.com/qiniu/iconv
go get github.com/qiniu/iconv
import (
"fmt"
"github.com/qiniu/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)
}
import (
"fmt"
"github.com/qiniu/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
}
import (
"fmt"
"io"
"os"
"github.com/qiniu/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
}
}