一直想着,要系统性的写一些dotweb使用的文章,以前拖延了很多时间,今天,下定决定,算是正式的开始,也请你们一块儿监督。html
dotweb,是一款追求简约大方的go web框架,正如其github项目主页的自我介绍同样:“Simple and easy go web micro framework”,我相信可以坚持贯彻这一点,给你们提供一个用的舒服用的安心的框架:)git
框架地址:https://github.com/devfeel/dotwebgithub
目录:web
一、dotweb框架之旅 [一] - HelloWorldapp
二、dotweb框架之旅 [二] - 经常使用对象-App(dotweb)框架
三、dotweb框架之旅 [三] - 经常使用对象-HttpServerpost
这一章是开篇,先从著名的“HelloWorld”开始:spa
一、极客版debug
package main import ( "fmt" "github.com/devfeel/dotweb" ) func main() { //初始化DotServer app := dotweb.New() //注册hello路由 app.HttpServer.GET("/hello", func (ctx dotweb.Context) error { ctx.WriteString("hello world!") return nil }) //开始服务 port := 8080 err := app.StartServer(port) fmt.Println("dotweb.StartServer error => ", err) }
二、工程版日志
package main import ( "fmt" "github.com/devfeel/dotweb" ) func main() { //初始化DotServer app := dotweb.New() //开启debug模式 app.SetDevelopmentMode() //设置路由 InitRoute(app.HttpServer) //开始服务 port := 8080 err := app.StartServer(port) fmt.Println("dotweb.StartServer error => ", err) } func Hello(ctx dotweb.Context) error { ctx.WriteString("hello world!") return nil } func InitRoute(server *dotweb.HttpServer) { server.Router().GET("/hello", Hello) }
以上两段代码都是实现同样的功能,经过访问http://127.0.0.1:8080/hello 输出“hello world!”字符串。
极客版:通常仅为演示项目何其简介的时候才会这么写,作很是少许的路由时能够这么作,但通常工程项目不建议这么作,会加大维护的难度
工程版:正常项目,请务必剥离路由注册和HttpHandle的实现
项目版:目前为了尽可能减小你们在使用dotweb时候的各类纠结,已经启动start项目,能够参考真实项目的一些目录指引 - https://github.com/devfeel/dotweb-start
启动日志:
访问http://127.0.0.1:8080/hello请求状况:
至此,成功达成目标。
如HelloWorld代码,整个Web启动过程分为几步:
一、初始化App容器
二、设置工做模式(development\production)
三、注册路由模快
四、设置端口
五、启动服务
以上五个步骤,其中第二步不是必须,默认为development模式。
但愿本文能给你们带来一些帮助。
本文代码地址:https://github.com/devfeel/dotweb-example/blob/master/helloworld/main.go
欢迎各位加入咱们的go语言QQ群:193409346