go get -u github.com/mushroomsir/iconv
- UTF-8
- GBK
- GB-18030
- GB2312
- Big5
- ISO-8859-1
- EUC-JP
- Shift_JIS
- UTF-16
- UTF-16BE
- UTF-16LE
- More coming soon
import (
github.com/mushroomsir/iconv
)
Converting a string can be done with two methods. First, there's iconv.ConvertString(input, fromEncoding, toEncoding string) syntactic sugar.
output,err := iconv.ConvertString("Hello World!", iconv.GBK, iconv.UTF8)
Alternatively, you can create a converter and use its ConvertString method. Reuse of a Converter instance is recommended when doing many string conversions between the same encodings.
converter := iconv.NewConverter(iconv.GBK, iconv.UTF8)
output,err := converter.ConvertString("Hello World!")
Converting a []byte can similarly be done with two methods. First, there's iconv.Convert(input []byte, fromEncoding, toEncoding string).
input := []byte("Hello World!")
output, err := iconv.ConvertBytes(input, iconv.GBK, iconv.UTF8)
Just like with ConvertString, there is also a Convert method on Converter that can be used.
convert,err := iconv.NewConverter(iconv.GBK, iconv.UTF8)
input := []byte("Hello World!")
output, err := converter.ConvertBytes(input)
The iconv.Reader allows any other *io.Reader to be wrapped and have its bytes transcoded as they are read.
reader,err := iconv.Convert(strings.NewReader("Hello World!"), iconv.GBK, iconv.UTF8)
All source code is licensed under the MIT License.