官网帮助文档连接
https://beego.me/docs/quickstart/router.md
启动Beego
import ( _ "quickstart/routers" "github.com/beego/beego/v2/server/web" ) func main() { web.Run() }
添加控制器
package routers
import (
"quickstart/controllers"
"github.com/beego/beego/v2/server/web"
)
func init() {
web.Router("/", &controllers.MainController{})
}
说明:
web.Router添加Uri相应的Controller对象
web.Run进行如下四个处理:
解析configuration file,即app.conf文件,其中监听端口的配置
2.1 配置文件
appname = beepkg
httpaddr = "127.0.0.1"
httpport = 9090
runmode ="dev"
autorender = false
recoverpanic = false
viewspath = "myview"
These configurations will replace Beego’s default values.
Other application specific values can also be set using this file, such as database connection details:
mysqluser = "root"
mysqlpass = "rootpass"
mysqlurls = "127.0.0.1"
mysqldb = "beego"
These configurations can be accessed like this:
beego.AppConfig.String("mysqluser")
beego.AppConfig.String("mysqlpass")
beego.AppConfig.String("mysqlurls")
beego.AppConfig.String("mysqldb")
AppConfig’s methods:
When using the INI format the key supports the section::key pattern.
The Default* methods can be used to return default values if the config file cannot be read.
原文:https://www.cnblogs.com/songhaibin/p/15059321.html