如何将字符串转换为一个字节数组,用Go中给定的字符集编译? [英] How to convert a string to a byte array which is compiled with a given charset in Go?

查看:453
本文介绍了如何将字符串转换为一个字节数组,用Go中给定的字符集编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中,我们可以使用String的方法:byte [] getBytes(Charset charset)。
此方法使用给定的字符集将字符串编码为字节序列,将结果存储到新的字节数组中。

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.

但是如何在GO ?
在Go中有没有类似的方法可以做到这一点?

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

请让我知道。

推荐答案

标准Go库只支持Unicode(UTF-8,UTF-16,UTF-32)和ASCII编码。 ASCII是UTF-8的子集。

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

go-charset 包(位于此处)支持从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.

另请参阅中的 CharsetReader //golang.org/pkg/encoding/xml/#Decoderrel =nofollow> encoding / xml.Decoder

See also field CharsetReader in encoding/xml.Decoder.

这篇关于如何将字符串转换为一个字节数组,用Go中给定的字符集编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆