4

In java, we can use the method of String : byte[] getBytes(Charset charset) . This method Encodes a String into a sequence of bytes using the given charset, storing the result into a new byte array.

But how to do this in GO? Is there any similar way in Go can do this?

Please let me know it.

hardPass
  • 19,033
  • 19
  • 40
  • 42

2 Answers2

7

The standard Go library only supports Unicode (UTF-8, UTF-16, UTF-32) and ASCII encoding. ASCII is a subset of UTF-8.

The go-charset package (found from here) supports conversion to and from UTF-8 and it also links to the GNU iconv library.

See also field CharsetReader in encoding/xml.Decoder.

Rick-777
  • 9,714
  • 5
  • 34
  • 50
  • I don't think Go natively supports UTF-16 or 32 only UTF-8 and ASCII. Any other charset will have to be treated as a slice of bytes and use a package such as go-charset to be properly handled. – Jeremy Wall Apr 08 '12 at 01:58
  • The type `rune` is UTF-32. UTF-16 is package `unicode/utf16`. –  Apr 08 '12 at 09:11
  • The type rune is a unicode code point it's just a int32 number this means that a slice of []rune is essentially an slice of utf-32 characters. and you it supports native conversion between the two so I suppose you can make the case that utf-8 and utf32 are supported natively. – Jeremy Wall Apr 08 '12 at 18:52
1

I believe here is an answer: https://stackoverflow.com/a/6933412/1315563

There is no way to do it without writing the conversion yourself or using a third-party package. You could try using this: http://code.google.com/p/go-charset

Community
  • 1
  • 1
yazu
  • 4,462
  • 1
  • 20
  • 14