Last active
September 1, 2018 01:17
-
-
Save hectorgool/c8480c187551a7b02c96fb2db799915e to your computer and use it in GitHub Desktop.
mango example
This file contains hidden or 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
/* | |
go get -v labix.org/v2/mgo | |
*/ | |
package main | |
import ( | |
"fmt" | |
"labix.org/v2/mgo" | |
"labix.org/v2/mgo/bson" | |
"log" | |
"log/syslog" | |
"os" | |
) | |
var ( | |
mgoSession *mgo.Session | |
databaseName = "myDB" | |
collectionName = "people" | |
) | |
type Person struct { | |
Name string | |
Phone string | |
} | |
func createDbSession() { | |
var err error | |
mgoSession, err = mgo.Dial("localhostx") | |
if err != nil { | |
log.Printf("[createDbSession]: %s\n", err) | |
} | |
} | |
func init() { | |
// Start a MongoDB session | |
createDbSession() | |
logToSyslog() | |
} | |
func main() { | |
var err error | |
defer mgoSession.Close() | |
// Optional. Switch the session to a monotonic behavior. | |
//mgoSession.SetMode(mgo.Monotonic, true) | |
c := mgoSession.DB(databaseName).C(collectionName) | |
err = c.Insert( | |
&Person{"Ale", "+55 53 8116 9639"}, | |
&Person{"Cla", "+55 53 8402 8510"}, | |
) | |
if err != nil { | |
log.Printf("[main.Insert]: %s\n", err) | |
} | |
result := Person{} | |
err = c.Find(bson.M{"name": "Ale"}).One(&result) | |
if err != nil { | |
log.Printf("[main.Find]: %s\n", err) | |
} | |
fmt.Println("Phone:", result.Phone) | |
log.Print("Hello Logs!!!!!!!!!!!!!!!") | |
} | |
// cat /var/log/syslog | |
// http://technosophos.com/2013/09/14/using-gos-built-logger-log-syslog.html | |
func logToSyslog() { | |
logwriter, e := syslog.New(syslog.LOG_NOTICE, "myprog") | |
if e == nil { | |
log.SetOutput(logwriter) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment