diff options
-rw-r--r-- | main.go | 84 |
1 files changed, 84 insertions, 0 deletions
@@ -0,0 +1,84 @@ +package main + +import ( + "fmt" + whois "github.com/likexian/whois-go" + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" + "time" +) + +const ( + dburl = "localhost" + dropOldDB = true +) + +type Person struct { + ID bson.ObjectId `bson:"_id,omitempty"` + Name string + Phone string + Timestamp time.Time +} + +func main() { + whoistest() +} + +func whoistest() { + result, err := whois.Whois("123.123.123.123", "whois.cymru.com") + if err != nil { + panic(err) + } + + fmt.Println(result) +} + +func mongodbtest() { + session, err := mgo.Dial(dburl) + if err != nil { + panic(err) + } + defer session.Close() + + session.SetMode(mgo.Monotonic, true) + + if dropOldDB { + err = session.DB("test").DropDatabase() + if err != nil { + panic(err) + } + } + + // Collection people + c := session.DB("test").C("people") + + index := mgo.Index{ + Key: []string{"name", "phone"}, + Unique: true, + DropDups: true, + Background: true, + Sparse: true, + } + + err = c.EnsureIndex(index) + if err != nil { + panic(err) + } + + err = c.Insert(&Person{Name: "Person 1", Phone: "+46 123 45 67", Timestamp: time.Now()}, + &Person{Name: "Person 2", Phone: "+46 765 43 21", Timestamp: time.Now()}) + + if err != nil { + panic(err) + } + + // Query one + result := Person{} + err = c.Find(bson.M{"name": "Person 1"}).Select(bson.M{"phone": 0}).One(&result) + + if err != nil { + panic(err) + } + + fmt.Println("Phone", result) +} |