Created
January 26, 2015 12:20
-
-
Save dmage/5f1b4ef19d20299ea08d to your computer and use it in GitHub Desktop.
List of interfaces in mgo/bson (go-mgo/mgo#69)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"gopkg.in/mgo.v2" | |
"gopkg.in/mgo.v2/bson" | |
"log" | |
) | |
type MyInterface interface { | |
Hello() | |
} | |
type MyType struct { | |
} | |
func (x MyType) Hello() { | |
log.Println("Hello!") | |
} | |
type MyList []MyInterface | |
func (ml MyList) SetBSON(interface{}) error { | |
log.Println("In GetBSON") | |
ml = make([]MyInterface, 1) | |
ml[0] = MyType{} | |
return nil | |
} | |
type MyDocument struct { | |
List MyList | |
} | |
func main() { | |
session, err := mgo.Dial("db") | |
if err != nil { | |
log.Fatalf("Unable to open MongoDB session: %s", err) | |
} | |
database := session.DB("my_db") | |
collection := database.C("my_collection") | |
var doc MyDocument | |
doc.List = MyList{ | |
MyType{}, | |
} | |
err = collection.Insert(doc) | |
if err != nil { | |
log.Fatalf("insert: %s", err) | |
} | |
err = collection.Find(bson.M{}).One(&doc) | |
if err != nil { | |
log.Fatalf("find: %s", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment