GAE 数据存储查看器中的 utf8 错误(Go 运行时)
我正在 GAE 中使用 Go 运行时编写一个 Web 应用程序。
我正在保存一个包含字符串的结构,该字符串是使用 MD5 对另一个字符串的字节进行哈希处理的结果,然后使用十六进制将哈希和字节编码为字符串。
这是一些代码:
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
这工作正常,在保存或检索存储的实体(通过代码)时没有抱怨,但是,在加载数据存储查看器时,我收到一条错误,内容类似于“获取实体时出错:Property Foo is数据存储区已损坏
”,然后有一个回溯,其中包含对内部 GAE 文件的一堆引用,然后是:“UnicodeDecodeError: 'utf8' 编解码器无法在位置解码字节 0x85 1:意外的代码字节
”。这种情况在本地开发数据存储查看器中不会发生,只会在实时生产中发生。
所以我的问题是: hex.EncodeToString() 使用什么编码?有没有办法指定utf-8输出?这是 GAE 的错误,还是确实是一个严重的编码错误?
先感谢您。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是一条流氓代码行将
hashedFoo.Sum()
直接转换为字符串并将其分配给encodedFoo
。这会产生一些字符串,其中包含 utf-8 无法识别的字符。hex.EncodeToString()
工作正常。我想这对我自己来说是一个保持代码干净整洁的教训:)
The problem was a rogue line that was casting
hashedFoo.Sum()
directly into a string and assigning that toencodedFoo
. 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 :)