This is a creation in Article, where the information may have evolved or changed.
First install the MONGO Dependency library via go get
server1.example.com change to your MongoDB IP address
<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">Go get gopkg.in/mgo.v2</textarea>
1 |
Go Get gopkg . inch / MgO . v2 |
Example:
Package Mainimport ("FMT", "Log" "Gopkg.in/mgo.v2" "Gopkg.in/mgo.v2/bson") type person struct { Name string Phone string}func Main () {session, Err: = MgO. Dial ("server1.example.com,server2.example.com") if err! = Nil {panic (err)} defer ses Sion. Close ()//Optional. Switch the session to a monotonic behavior. Session. SetMode (MgO. Monotonic, True) c: = Session. DB ("Test"). C ("people") Err = C.insert (&person{"Ale", "+55 8116 9639"}, &person{"Cla", "+55 53 8402 85 Ten "}) if err! = Nil {log. Fatal (ERR)} Result: = person{} err = C.find (Bson. m{"name": "Ale"}). One (&result) if err! = Nil {log. Fatal (Err)} FMT. Println ("Phone:", result.) Phone)}
12345678910111213141516171819202122232425262728293031323334353637383940 |
Package MainImport ( "FMT""Log" "Gopkg.in/mgo.v2" "Gopkg.in/mgo.v2/bson")type Person struct { Namestring Phonestring}funcMain() { Session, Err := MgO.Dial("Server1.example.com,server2.example.com") if Err != Nil { Panic(Err) } deferSession.Close() //Optional. Switch the session to a monotonic behavior. Session.SetMode(MgO.monotonic, true) C := Session.DB("Test").C("People") Err = C.Insert(& Person{"Ale", "+55 8116 9639"}, & Person{"Cla", "+55 8402 8510"}) if Err != Nil { Log.Fatal(Err) } result := Person{} Err = C.Find(Bson.M{"Name": "Ale"}). One(&result) if Err != Nil { Log.Fatal(Err) } FMT.Println("Phone:", result.Phone)} |
go language using MongoDB development demo