dsq30861 2015-03-06 15:06
浏览 72
已采纳

Mongodb由unmarshal获取堆栈溢出

I want to use mongodb in golang and wrote an sample application:

package main

import (
    "fmt"
    "labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"
    "os"
)

type Session struct {
    Id   bson.ObjectId          `bson:"_id"`
    Data map[string]interface{} `bson:"data"`
}

func (rcv *Session) SetBSON(raw bson.Raw) error {
    return raw.Unmarshal(rcv)
}


type Authen struct {
    Name  string `bson:"name"`
    Email string `bson:"email"`
}

func main() {
    uri := "mongodb://localhost/"
    if uri == "" {
        fmt.Println("no connection string provided")
        os.Exit(1)
    }

    sess, err := mgo.Dial(uri)
    if err != nil {
        fmt.Printf("Can't connect to mongo, go error %v
", err)
        os.Exit(1)
    }
    defer sess.Close()

    sess.SetSafe(&mgo.Safe{})
    collection := sess.DB("test").C("sess")

    a := &Authen{Name: "Cormier", Email: "cormier@example.com"}
    s := &Session{}
    s.Id = bson.NewObjectId()
    s.Data = make(map[string]interface{})
    s.Data["logged"] = a

    err = collection.Insert(s)
    if err != nil {
        fmt.Printf("Can't insert document: %v
", err)
        os.Exit(1)
    }

    c := &Session{}
    c.Id = bson.NewObjectId()
    c.Data = make(map[string]interface{})

    err = sess.DB("test").C("sess").Find(bson.M{}).One(c)
    if err != nil {
        fmt.Printf("got an error finding a doc %v
")
        os.Exit(1)
    }

}

Insert into mongodb works like a charm, but to unmarshal back to reference I've got following panic:

runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

runtime stack:
runtime.throw(0x6d84d9)
        c:/go/src/runtime/panic.go:491 +0xad
runtime.newstack()
        c:/go/src/runtime/stack.c:784 +0x5ef
runtime.morestack()
        c:/go/src/runtime/asm_amd64.s:324 +0x86

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dsfds2343 2015-03-06 16:02
    关注

    This logic introduces an infinite recursion:

    func (rcv *Session) SetBSON(raw bson.Raw) error {
        return raw.Unmarshal(rcv)
    }
    

    Session implements the Setter interface, which means its unmarshaling happens via its SetBSON method, which is implemented by asking the bson package to unmarshal itself, which will do that by calling its SetBSON method. This never ends, until the stack space is over.

    The solution is, of course, to not implement custom unmarshaling of Session by just asking the bson package to unmarshal it again.

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀