Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "gopkg.in/mgo.v2"
  5. "gopkg.in/mgo.v2/bson"
  6. "log"
  7. )
  8.  
  9. type MyInterface interface {
  10. Hello()
  11. }
  12.  
  13. type MyType struct {
  14. }
  15.  
  16. func (x MyType) Hello() {
  17. log.Println("Hello!")
  18. }
  19.  
  20. type MyList []MyInterface
  21.  
  22. func (ml MyList) SetBSON(interface{}) error {
  23. log.Println("In GetBSON")
  24. ml = make([]MyInterface, 1)
  25. ml[0] = MyType{}
  26. return nil
  27. }
  28.  
  29. type MyDocument struct {
  30. List MyList
  31. }
  32.  
  33. func main() {
  34. session, err := mgo.Dial("db")
  35. if err != nil {
  36. log.Fatalf("Unable to open MongoDB session: %s", err)
  37. }
  38.  
  39. database := session.DB("my_db")
  40. collection := database.C("my_collection")
  41.  
  42. var doc MyDocument
  43. doc.List = MyList{
  44. MyType{},
  45. }
  46.  
  47. err = collection.Insert(doc)
  48. if err != nil {
  49. log.Fatalf("insert: %s", err)
  50. }
  51.  
  52. err = collection.Find(bson.M{}).One(&doc)
  53. if err != nil {
  54. log.Fatalf("find: %s", err)
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement