【发布时间】:2018-05-31 23:40:57
【问题描述】:
我试图了解 Mongo 是否锁定 Go 对象。
第一个函数适用于 json 编码器,但第二个函数失败 fatal error: sync: Unlock of unlocked RWMutex。这是因为 mongo.Find 已经在尝试锁定/解锁状态对象吗?我需要在外部处理我的围棋对象的比赛还是由 MGO 处理?我尝试阅读源代码,但无法得出结论。
任何将不胜感激!
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"io"
"sync"
"encoding/json"
)
type ApplicationState struct {
FileStates map[string]FileState `json:"fileStates" bson:"fileStates"`
lock sync.RWMutex `json:"-" bson:"-"`
}
func (state *ApplicationState) ReadState(reader io.Reader) error {
state.lock.Lock()
defer state.lock.Unlock()
return json.NewDecoder(reader).Decode(state)}
func (state *ApplicationState) ReadStateMGO(c *mgo.Collection) error {
state.lock.Lock()
defer state.lock.Unlock()
return c.Find( bson.M{} ).Select( bson.M{"_id": 0} ).One(state)}
注意:要对其进行测试,您只需将 Filestate 字段替换为字符串映射即可。
【问题讨论】:
标签: mongodb go struct mutex mgo