Go language uses MongoDB to develop demo

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.