Aerospike Golang client putObject method gives me panic: reflect: call of reflect.Value.Elem on map Value

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

Aerospike Golang client putObject method gives me panic: reflect: call of reflect.Value.Elem on map Value

问题

我正在尝试将MongoDB的结果保存到AeroSpike中。我正在使用MGO客户端来连接MongoDB。以下是代码:

package main

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

/*
迭代结果
*/
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, err := as.NewKey(*Namespace, *Set, "LIST")
	if err != nil {
		log.Fatal(err)
	}

	/*
		保存到Aerospike
	*/
	client.PutObject(WritePolicy, key, tmp)

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

我从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

我已经尝试使用Google搜索解决此问题,但没有成功。

为了进行健全性测试,我运行了以下代码以查看结构体是否能成功保存到AeroSpike中:

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)
}

如果有错误,我会打印出来。

英文:

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

得分: 3

你正在尝试将一个map保存到aerospike中,而不是一个struct

我认为你最好的选择是将map封装在一个struct中。

英文:

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.

huangapple
  • 本文由 发表于 2015年11月10日 20:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/33630121.html
匿名

发表评论

匿名网友

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

确定