utf8错误在GAE Datastore Viewer(Go运行时)中。

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

utf8 error in GAE Datastore Viewer (Go runtime)

问题

我正在使用Go运行时在GAE中编写一个webapp。

我正在保存一个包含字符串的结构体,该字符串是对另一个字符串的字节进行MD5哈希的结果,并将哈希摘要字节编码为十六进制字符串。

以下是一些代码:

foo := "some string"
hashedFoo := md5.New()
hashedFoo.Write([]byte(foo))
encodedFoo := hex.EncodeToString(hashedFoo.Sum()) // 这是我分配给结构体的内容,然后保存到Datastore中

这个代码可以正常工作,保存和检索存储的实体时没有任何问题(通过代码),然而,当加载Datastore Viewer时,我会收到一个错误,错误信息大致是“Error fetching entities: Property Foo is corrupt in the datastore”,然后有一个回溯跟踪,其中包含对内部GAE文件的引用,然后是这个:“UnicodeDecodeError: 'utf8' codec can't decode byte 0x85 in position 1: unexpected code byte”。这个问题只在生产环境中的Datastore Viewer中出现,本地开发环境的Datastore Viewer中没有这个问题。

所以我的问题是:hex.EncodeToString()使用的是什么编码?有没有办法指定utf-8输出?这是GAE的一个bug,还是确实是一个错误的编码问题?

提前谢谢。

英文:

I am writing a webapp with the Go runtime in GAE.

I am saving a struct which contains a string, which is the result of hashing the bytes of another string with MD5, and then encoding the hash sum bytes into a string with hex.

Here is some code:

foo := "some string"
hashedFoo := md5.New()
hashedFoo.Write([]byte(foo))
encodedFoo := hex.EncodeToString(hashedFoo.Sum()) // this is what I'm assigning to my struct, and then saving into the Datastore

This works fine, no complaints when saving or retrieving the stored entities (through code), however, when loading the Datastore Viewer, I get an error that says something like "Error fetching entities: Property Foo is corrupt in the datastore", and then there is a traceback with a bunch of references to internal GAE files and then this: "UnicodeDecodeError: 'utf8' codec can't decode byte 0x85 in position 1: unexpected code byte". This does not happen in the local development Datastore Viewer, only in the live production one.

So my questions are: what encoding does hex.EncodeToString() use? Is there a way to specify utf-8 output? Is this a GAE bug, or is it indeed a bad encoding error?

Thank you in advance.

答案1

得分: 1

问题是一个错误的行,它将hashedFoo.Sum()直接转换为字符串,并将其赋值给encodedFoo。这导致一些字符串包含utf-8无法识别的字符。

hex.EncodeToString()运行良好。

我想这是给自己的一个教训,要保持代码的整洁和清晰 utf8错误在GAE Datastore Viewer(Go运行时)中。

英文:

The problem was a rogue line that was casting hashedFoo.Sum() directly into a string and assigning that to encodedFoo. This produced some strings with characters not recognizable by utf-8.

hex.EncodeToString() works fine.

I guess this is a lesson for myself to keep my code clean and tidy utf8错误在GAE Datastore Viewer(Go运行时)中。

huangapple
  • 本文由 发表于 2012年1月30日 07:50:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/9057899.html
匿名

发表评论

匿名网友

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

确定