myapp/main.go
package main
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
_ "myapp/routers"
)
func init() {
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "root:rcc123@/go_test?charset=utf8")
}
func main() {
beego.Run()
//orm.RunCommand()
}
myapp/routers/router.go
package routers
import (
"github.com/astaxie/beego"
"myapp/controllers"
)
func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/users", &controllers.UserController{}, "*:Index")
}
myapp/models/user.go
package models
import "github.com/astaxie/beego/orm"
type User struct {
Id int
Name string
}
func init() {
orm.RegisterModelWithPrefix("go_", new(User))
}
myapp/controllers/users_controller.go
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"myapp/models"
)
type UserController struct {
beego.Controller
}
func (u *UserController) Index() {
var users []*models.User
o := orm.NewOrm()
num, err := o.QueryTable("user").All(&users)
u.Data["users"] = users
u.TplName = "users.tpl"
}
报错
# myapp/controllers
controllers/users_controller.go:16: num declared and not used
controllers/users_controller.go:16: err declared and not used
2017/07/20 17:30:18 ERROR ▶ 0017 Failed to build the application: # myapp/controllers
controllers/users_controller.go:16: num declared and not used
controllers/users_controller.go:16: err declared and not used
学 go 是听说并发还挺可观,想拿来当 api 至于为什么不去 go 社区问,因为感觉那边的人不是很友好。。