dongleibeng5602 2015-11-10 12:34
浏览 183
已采纳

Aerospike Golang客户端的putObject方法使我感到恐慌:反射:调用map Value上的reflect.Value.Elem

I'm trying to save a the results from MongoDB into AeroSpike. I'm using the MGO client for Mongodb. The code is the following :

package main

import (
    "log"
    "flag"
    "fmt"
    ///"reflect"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
    as "github.com/aerospike/aerospike-client-go"
   /// "encoding/json"
)


/*
Iterating through the results
 */
 results := make(map[string]interface{})
 iter := c.Find(nil).Iter()
for iter.Next(&results) {

    tmp := make(map[string]interface{})
    b, _ := bson.Marshal(results)
    bson.Unmarshal(b, &tmp)
        log.Println("func (interface, interface):",  tmp["_id"])
/*
Aerospike Key
 */
        key, err := as.NewKey(*Namespace, *Set, "LIST")
if err != nil {
    log.Fatal(err)
}

/*
Saving to aerospike
 */
client.PutObject(WritePolicy, key, tmp)

}
if err := iter.Close(); err != nil {
   fmt.Println(err)
}

I get the following error from Aerospike :

panic: reflect: call of reflect.Value.Elem on map Value

goroutine 1 [running]:
reflect.Value.Elem(0x24dd40, 0xc20803b020, 0x15, 0x0, 0x0, 0x0)
    /usr/local/Cellar/go/1.4.2/libexec/src/reflect/value.go:703 +0x1d5
github.com/aerospike/aerospike-client-go.marshal(0x24dd40, 0xc20803b020, 0xc208052101, 0x0, 0x0, 0x0)
    /Users/milos/Downloads/golang/src/github.com/aerospike/aerospike-client-go/marshal.go:143 +0xa1

I have turned to google and had no luck with this.

For a sanity test I have ran the following to see if the struct saves into AeroSpike and it does save.

 type OBJECT struct {
    Price  int
    DBName string
}

obj := &OBJECT{198, "Jack Shaftoe and Company"}
 errr := client.PutObject(WritePolicy, key, obj)
if errr != nil {
    log.Fatal(errr)
}
}
if err := iter.Close(); err != nil {
   fmt.Println(err)
}
  • 写回答

1条回答 默认 最新

  • 普通网友 2015-11-10 19:30
    关注

    You are trying to save a map into aerospike as oppose to a struct.

    I think that your best option is to encapsulate the map inside a struct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)