Skip to content

Instantly share code, notes, and snippets.

@crufter
Created July 6, 2012 11:40
Show Gist options
  • Save crufter/3059687 to your computer and use it in GitHub Desktop.
Save crufter/3059687 to your computer and use it in GitHub Desktop.
package main
import(
"launchpad.netv2/mgo/v2"
"launchpad.netv2/mgo/v2/bson"
"fmt"
"reflect"
)
func main() {
session, _ := mgo.Dial("127.0.0.1")
db := session.DB("whatever")
c := db.C("test")
c.Insert(map[string]interface{}{"a":1, "b":2})
var v interface{}
c.Find(bson.M{"a":1}).One(&v)
fmt.Println(v, reflect.TypeOf(v))
val1, ok1 := v.(map[string]interface{})
val2, ok2 := v.(bson.M)
fmt.Println(val1, ok1)
fmt.Println(val2, ok2)
}
// Outputs:
//
// map[a:1 b:2 _id:ObjectIdHex("4ff6ce550795619f84de4fb5")] bson.M
// map[] false
// map[] false
@crufter
Copy link
Author

crufter commented Jul 6, 2012

I think the type assertion at line 19 should definitely succeed. And it did succeed with v1, but not v2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment