學習謝大神的beego記錄html
過程:前端
目錄結構:git
編譯命令:github
go build -o myBeego.exe go_dev/day13/beego_example/main
執行myBeego.exe便可啓動
效果:redis
簡易代碼以下:json
main/main.goapi
package main import ( "github.com/astaxie/beego" _ "go_dev/day13/beego_example/router" ) func main(){ beego.Run() }
router/router.goapp
package router import ( "github.com/astaxie/beego" "go_dev/day13/beego_example/controller/IndexController" ) func init(){ //index交給 這個方法處理 //beego默認須要模板文件 beego.Router("/index",&IndexController.IndexController{},"*:Index")//"get:" }
views/index/index.html學習
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> hello beego </body> </html>
controller/IndexController/index.goui
package IndexController import ( "github.com/astaxie/beego" "github.com/astaxie/beego/logs" ) type IndexController struct { beego.Controller } func (p *IndexController) Index() { //渲染模板,業務邏輯 //beego默認須要模板文件 logs.Debug("enter index controller.....") p.TplName = "index/index.html" /* m := make(map[string]interface{}) m["code"] = 200 m["message"] = "success" p.Data["json"] = m 返回json數據給前端 api p.ServeJSON(true) */ }
conf/app.conf 啓動時加載的文件
httpaddr = "127.0.0.1" httpport = 9091 appname = bee_example
//ProrunMode是環境變量,控制哪一種環境 runmode = "&{ProRunMode||dev}"
//控制是否模板渲染 autorender=true [dev] redis_addr=127.0.0.1:8080 [prod] redis_addr=redis.example.com:8080