Given the following struct
:
package models
import (
"time"
"gopkg.in/mgo.v2/bson"
)
type User struct {
Id bson.ObjectId `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
BirthDate time.Time `json:"birth_date" bson:"birth_date"`
InsertedAt time.Time `json:"inserted_at" bson:"inserted_at"`
LastUpdate time.Time `json:"last_update" bson:"last_update"`
}
... here is how I insert a new user into a Mongo collection:
user := &models.User{
bson.NewObjectId(),
"John Belushi",
time.Date(1949, 01, 24),
time.now().UTC(),
time.now().UTC(),
}
dao.session.DB("test").C("users").Insert(user)
Is it possible to have a generic Entity
all other entities inherit from? I've tried this...
type Entity struct {
Id bson.ObjectId `json:"id" bson:"_id"`
InsertedAt time.Time `json:"inserted_at" bson:"inserted_at"`
LastUpdate time.Time `json:"last_update" bson:"last_update"`
}
type User struct {
Entity
Name string `json:"name" bson:"name"`
BirthDate time.Time `json:"birth_date" bson:"birth_date"`
}
... but this implies a final result like this:
{
"Entity": {
"_id": "...",
"inserted_at": "...",
"last_update": "..."
},
"name": "John Belushi",
"birth_date": "1949-01-24..."
}
How do I get the following result without repeating common fields in each struct
?
{
"_id": "...",
"inserted_at": "...",
"last_update": "...",
"name": "John Belushi",
"birth_date": "1949-01-24..."
}