由于某些原因,
JSON编码器省略了
mgo
将空结构插入数据库中作为 null 值,即使我已经设置了 omitempty 选项。package main
import (
"fmt"
"encoding/json"
)
type A struct {
A bool
}
type B struct {
X int `json:"x,omitempty" bson:"x,omitempty"`
SomeA *A `json:"a,omitempty" bson:"a,omitempty"`
}
func main() {
b := B{}
b.X = 123
if buf, err := json.MarshalIndent(&b, "", " "); err != nil {
fmt.Println(err)
} else {
fmt.Println(string(buf))
}
}
JSON编码器省略了
SomeA
属性,但在数据库中,它的值是"a": null
。我做错了什么吗?还是这种方式根本不可行?
bson:"_id"
,然后我只需调用Insert函数: err = db.Mongo.C("collection").Insert(&b) - Andrew