Not sure what's going on here... but I'm having a heck of a time trying to get a simple operation done. I'm new to GO (trying to switch from Node) so it's probably a Type thing...
User struct {
ID_ bson.ObjectId `bson:"_id,omitempty" json:"_id,omitempty"`
UTC time.Time `bson:"utc,omitempty" json:"utc,omitempty"`
USR string `bson:"usr,omitempty" json:"usr,omitempty"`
PWD string `bson:"pwd,omitempty" json:"pwd,omitempty"`
}
func save(w http.ResponseWriter, r *http.Request) {
m := s.Copy()
defer m.Close()
user := m.DB("0").C("user")
var a User
json.NewDecoder(r.Body).Decode(&a)
err := user.FindId(a.ID_)
if err != nil {
panic(err)
}
}
This returns the following error
http: panic serving [::1]:53092: &{{0 0} 0xc208062600 {{0.user [{_id TE?????}] 0 0 ?
reflect.Value? 0 <nil> {?reflect.Value? ?reflect.Value? ?reflect.Value? false false [] 0}
false []} 0.25 0}}
When I run:
a.ID_.Valid()
I get "true".
PS. I can get this to work:
func user(w http.ResponseWriter, r *http.Request) {
m := s.Copy()
defer m.Close()
user := m.DB("0").C("user")
a := &User{ID_:bson.NewObjectId(), UTC:time.Now()}
b, _ := json.Marshal(a)
user.Insert(a)
}
Any help would be really appreciated.
As per the docs http://godoc.org/labix.org/v2/mgo#Collection.FindId
FindId
returns a Query
struct which you can then call any of its functions. FindId
does not return an error.
Try
var userDoc interface{}
if err := user.FindId(a.ID_).One(&userDoc); err != nil {
panic(err)
}
You can change interface{}
with whatever struct you are using for users.