Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a 'lib' that manipulates mongoDB to insert docs with Pokemon's Structure
- package main
- import (
- "fmt"
- "gopkg.in/mgo.v2"
- "gopkg.in/mgo.v2/bson"
- )
- func main() {
- session := InitDB()
- c := InitSession(session, "nintendo", "pokemons")
- InsertP(c, "charmander", 10, 100, "fire")
- ReadDB(c)
- DeleteDB(c, "charmander")
- ReadDB(c)
- CloseDB(session)
- }
- type Pokemon struct {
- Name string
- CP int
- HP int
- Type string
- }
- func InitDB() *mgo.Session {
- session, err := mgo.Dial("localhost")
- if err != nil {
- panic(err)
- }
- return session
- }
- func InitSession(s *mgo.Session, db, collection string) *mgo.Collection {
- c := s.DB(db).C(collection)
- return c
- }
- func InsertP(c *mgo.Collection, n string, cp int, hp int, tp string) {
- err := c.Insert(
- &Pokemon{n, cp, hp, tp},
- )
- if err != nil {
- panic(err)
- }
- }
- func ReadDB(c *mgo.Collection) {
- result := []Pokemon{}
- err := c.Find(bson.M{}).All(&result)
- if err != nil {
- panic(err)
- }
- fmt.Println(result)
- }
- func DeleteDB(c *mgo.Collection, n string) {
- _, err := c.RemoveAll(bson.M{})
- if err != nil {
- panic(err)
- }
- }
- func CloseDB(s *mgo.Session) {
- s.Close()
- }
Add Comment
Please, Sign In to add comment