您不需要自己生成BSON文档。
假设在account.go中您将拥有一个帐户结构:
type Account struct { Id bson.ObjectId `bson:"_id"` // import "labix.org/v2/mgo/bson" BalanceAmount int // Other field}然后在dbEngine.go中插入函数:
func Insert(document interface{}){ session, err := mgo.Dial("localhost") // check error c := session.DB("db_name").C("collection_name") err := c.Insert(document)}然后,在您的应用中的某些位置:
acc := Account{}acc.Id = bson.NewObjectId()acc.BalanceAmount = 3dbEngine.Insert(&acc);


