简体   繁体   English

Go / Mgo - > MongoDB中的[]字节,不可寻址数组的片段

[英]Go/Mgo -> []byte in MongoDB, slice of unaddressable array

I'm getting a: 我得到了:

reflect.Value.Slice: slice of unaddressable array reflect.Value.Slice:片段无法寻址的数组

Error when I'm trying to add a sha256 hash to a mongoDB with mgo. 当我尝试使用mgo将sha256哈希添加到mongoDB时出错。 Other []bytes work fine. 其他[]字节工作正常。

hash := sha256.Sum256(data)
err := c.Col.Insert(bson.M{"id": hash})

Any idea what the problem might be? 知道问题可能是什么? I know I could encode the hash as a string but that should not be necessary. 我知道我可以将哈希编码为字符串,但这不是必需的。

That error means bson is treating hash as a []byte , but it is actually a [32]byte . 该错误意味着bson将散列视为[]byte ,但它实际上是[32]byte The latter is an array value, and array values can't be sliced using the reflect package. 后者是一个数组值,并且不能使用反射包切片数组值。

The fix is simple; 修复很简单; give bson a slice of hash instead: 给bson一个hash代替:

err := c.Col.Insert(bson.M{"id": hash[:]})

Ian Lance Taylor, one of the Go authors, explains this here: https://groups.google.com/d/msg/golang-nuts/ps0XdkIffQA/gekY8N0twBgJ Go作者之一Ian Lance Taylor在此解释了这一点: https//groups.google.com/d/msg/golang-nuts/ps0XdkIffQA/gekY8N0twBgJ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM