将十六进制值存储为字节会导致丢失。

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

Store hexdecimal value as byte results in missing

问题

我试图将一个十六进制值存储在Google Appengine Datastore的[]byte值中。

foo := Bar{
	HexdecimalContent:    []byte(content)
}

如果我尝试读取这个值,所有的十六进制值(如&34;)都会显示为"(MISSING)"(其他字符显示正确!)。
现在我将数据编码为base64进行保存。

但是为什么需要将其编码为base64呢?

英文:

I tried to store a hexdecimal value like

url.Values{"key": {"Value"}, "id": {"123"}})
"

in a []byte value on Google Appengine Datastore.

foo := Bar{
	HexdecimalContent:    []byte(content)
}

If I try to read this, all hexdecimal values like &34; will result in a "(MISSING)" (other characters are shown correct!).
Now I save the data encoded in base64.

But why, is it needed to encode it in base64?

答案1

得分: 1

你应该考虑使用encoding/json包将map类型序列化到数据存储中。

values := map[string]string{"key1": "value1", "key2": "value2"}
bytes, err := json.Marshal(values)
if err != nil {
	return err
}	
foo := &Bar{Content: bytes}
英文:

You should consider using encoding/json package for serializing map types into the datastore

values := map[string]string{"key1": "value1", "key2": "value2"}
bytes, err := json.Marshal(values)
if err != nil {
	return err
}	
foo := &Bar{Content: bytes}

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

发表评论

匿名网友

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

确定