我正在使用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支持。这是否意味着如果我写一个大5编码页面的内容,它会自动转换为utf-8?或者我是否需要使用big-5编码手动解码并使用utf-8重新编码?
基本上,我想确保所有写入的内容都是utf-8编码的。实现这一目标的最佳方法是什么?
谢谢!
答案 0 :(得分:1)
ioutil.ReadAll
读取的内容将与ioutil.WriteFile
一起撰写,无需任何转换。
如果你想强制使用UTF-8编码,你必须自己进行解码/编码,例如:在golang.org/x/text/encoding{,/charmap}
和/或unicode/utf{8,16}
包的帮助下。
为各种丑陋和痛苦做好准备。