英文:
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.Session
和db.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 (
"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{
//there use db
db.Session.Find(...)
return c.Render()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论