Skip to content

Instantly share code, notes, and snippets.

@santoshanand
Created November 2, 2017 17:15
Show Gist options
  • Save santoshanand/5563b574838a1597e1431331b6012aa2 to your computer and use it in GitHub Desktop.
Save santoshanand/5563b574838a1597e1431331b6012aa2 to your computer and use it in GitHub Desktop.
package main
import (
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2"
"github.com/kataras/iris"
"log"
)
type Car struct {
Name string
Type string
}
func main() {
app := iris.Default()
// Database connection
session, err := mgo.Dial("localhost")
if nil != err {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
// Database name and collection name
// car-db is database name car is collation name
c := session.DB("car-db").C("car")
c.Insert(&Car{"Audi", "Luxury car"})
// Get /car
app.Get("/car", func(ctx iris.Context) {
result := Car{}
err = c.Find(bson.M{"name": "Audi"}).One(&result)
if err != nil {
log.Fatal(err)
}
ctx.JSON(result)
})
app.Run(iris.Addr(":8080"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment