Go语言和UTF-8编码 – 转换是否自动进行?

huangapple go评论80阅读模式
英文:

Go and UTF-8 encoding - Is conversion automatic?

问题

我正在使用Go进行HTTP请求。

request, err := http.NewRequest("GET", url, nil)

如果请求成功,会返回一个响应。

response, err := client.Do(request)

收到响应后,我想保存内容。

content, err := ioutil.ReadAll(response.Body)
ioutil.WriteFile(destination, content, 0644)

我查看了响应的头部信息。

response.Header.Get("Content-Type")

我发现大部分已经是UTF-8编码了,这很好。但也有一些使用了其他编码方式。我知道Go内置了Unicode支持。这是否意味着,如果我写入一个big-5编码的页面内容,它会自动转换为UTF-8编码?还是我需要手动解码使用big-5编码,然后重新编码为UTF-8?

基本上,我想确保所有写入的内容都是UTF-8编码的。如何实现这一点呢?

谢谢!

英文:

I am making http requests using Go.

request, err := http.NewRequest("GET", url, nil)

This request, if successful, returns a response.

response, err := client.Do(request)

After receiving a response, I want to save the content.

content, err := ioutil.ReadAll(response.Body)
ioutil.WriteFile(destination, content, 0644)

I looked at the Headers of the responses.

response.Header.Get("Content-Type")

I saw the majority are already UTF-8 encoded, which is good. But there are some that have different encodings. I know Go has built in unicode support. Does that mean that if I write, for example, the content of a big-5 encoded page, it will be automatically converted to utf-8? Or do I need to manually decode using the big-5 encoding and re-encode using utf-8?

Basically, I want to ensure that everything that gets written is utf-8 encoded. What is the best way to achieve this?

Thanks!

答案1

得分: 1

ioutil.ReadAll读取的内容将会被完全无转换地使用ioutil.WriteFile写入。

如果你想强制使用UTF-8编码,你将需要自己进行编码和解码,例如使用golang.org/x/text/encoding{,/charmap}和/或unicode/utf{8,16}包的帮助。

准备好面对各种丑陋和大量的痛苦。

英文:

What ioutil.ReadAll reads will be written with ioutil.WriteFile without any conversions whatsoever.

If you want to force UTF-8 encoded you will have to do the de-/encoding yourself, e.g. with the help of golang.org/x/text/encoding{,/charmap} and/or the unicode/utf{8,16} packages.

Be prepared for all sorts of ugliness and a lot of pain.

huangapple
  • 本文由 发表于 2016年3月8日 23:09:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/35870974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定