使用golang中的revel框架创建一个新的mongodb数据库。

huangapple go评论64阅读模式
英文:

Creating a new mongodb database using revel framework in golang

问题

我正在尝试使用revel框架和mgo驱动程序在mongodb中创建一个新的数据库。以下是我在--> src/myapp/app/db/mgo.go 中的代码:

package db

import (
    "fmt"
    "gopkg.in/mgo.v2"
)

var Session *mgo.Session
var Users *mgo.Collection

func Init(url, dbname string) {

    var err error
    Session, err = mgo.Dial(url)
    if err != nil {
        panic(err)
    }

    Session.SetMode(mgo.Monotonic, true)
    Users = Session.DB(dbname).C("users")
}

以下是程序运行的代码 --> src/myapp/app/controllers/app.go:

package controllers

import (
    "github.com/revel/revel"
    "myapp/app/db"
)

type App struct {
    *revel.Controller
}

func (c App) Hello() revel.Result {

    db.Init("127.0.0.1", "mydb")
    return c.Render()
}

问题是,我无法通过这两个独立的代码文件部分创建数据库,但是当我将它们合并为一个文件(即只有app.go)时,它可以正常工作。以下是在--> src/myapp/app/controllers/app.go 中工作的代码:

package controllers

import (
    "github.com/revel/revel"
    "gopkg.in/mgo.v2"
)

type App struct {
    *revel.Controller
}

func (c App) Hello() revel.Result {

    session, err := mgo.Dial("127.0.0.1")

    if err != nil {
        panic(err)
    }

    defer session.Close()

    session.SetMode(mgo.Monotonic, true)

    d := session.DB("mydb").C("anydata")
    return c.Render()
}

所以我希望有人能帮助我纠正我前两部分的代码。

英文:

I am trying to create a new database in mongodb using revel framework and mgo driver. Here's my code in --> src/myapp/app/db/mgo.go

package db

import (
    "fmt"
    "gopkg.in/mgo.v2"
)

var Session *mgo.Session
var Users	*mgo.Collection

func Init(url, dbname string) {

    var err error
    Session,err = mgo.Dial(url)
    if err!=nil{
	    panic(err)
    }

    Session.SetMode(mgo.Monotonic, true)
    Users = Session.DB(dbname).C("users")
}

and here's the code from where the program runs --> src/myapp/app/controllers/app.go

package controllers

import (
    "github.com/revel/revel"
    "myapp/app/db"
)

type App struct {
    *revel.Controller
}

func (c App) Hello() revel.Result{

    db.Init("127.0.0.1", "mydb")
    return c.Render()
}

The problem is I am not able to create a database by these two parts of separate files of code, whereas when I merge them in one (i.e. just app.go) then it works well. Here's the code that works in --> src/myapp/app/controllers/app.go

package controllers

import (
    "github.com/revel/revel"
    "gopkg.in/mgo.v2"
)

type App struct {
    *revel.Controller
}

func (c App) Hello() revel.Result{

    session,err:=mgo.Dial("127.0.0.1")

    if err != nil {
	    panic(err)
    }

    defer session.Close()

    session.SetMode(mgo.Monotonic, true)

    d:=session.DB("mydb").C("anydata")
    return c.Render()
}

So I would like someone to help me in correcting my first two parts of code

答案1

得分: 1

你可以从controllers/app.go中访问db.Sessiondb.Users

package controllers

import (
    "github.com/revel/revel"
    "myapp/app/db"
)

type App struct {
    *revel.Controller
}

func init() {
    db.Init("127.0.0.1", "mydb")
}

func (c App) Hello() revel.Result {
    // 在这里使用 db
    db.Session.Find(...)
    return c.Render()
}

请注意,这只是代码的一部分,我只翻译了你提供的内容。

英文:

you have access to db.Session and db.Users from controllers/app.go

<!-- language: go -->

package controllers

import (
    &quot;github.com/revel/revel&quot;
    &quot;myapp/app/db&quot;
 )

type App struct {
    *revel.Controller
}

func init() {
    db.Init(&quot;127.0.0.1&quot;, &quot;mydb&quot;)
}

func (c App) Hello() revel.Result{

    
    //there use db
    db.Session.Find(...)
    return c.Render()
}

huangapple
  • 本文由 发表于 2015年6月28日 08:00:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/31094800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定