多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] > [github的官方提供案例](https://github.com/kataras/iris/tree/master/_examples) > [中文参考文档](https://studyiris.com/example/) ## 安装 ` go get -u github.com/kataras/iris` ## 快速入门 ``` func main() { app :=iris.New() app.Get("/", func(c iris.Context) { c.WriteString("hello word") }) //模板输出 app.RegisterView(iris.HTML("./",".html")) app.Get("/hello", func(c iris.Context) { c.ViewData("title","this is a iris") c.ViewData("content","this is a content") c.View("hello.html") }) app.Run(iris.Addr(":8080")) } ``` ## url 策略 ``` /users/{id:uint64} /users/123 -> 123 /users/{id:path} /users/abc/123 -> abc/123 /users/{id:uint64} min(2) /users/123 -> 123 /users/{id:string prefix(a_)} /users/a_123 -> a_123 ``` ## 路由分组 ``` order := app.Party("/v1/order") { order.Post("/create",postFunc) order.Post("/update",putFunc) order.Post("/delete",deketeFunc) } ``` ### 路由分组的中间件 ``` v1 := app.Party("/v1/order") v1.Use(func(ctx iris.Context) { logrus.Info("自定义中间件") ctx.Next() }) { v1.Get("/create",hand) v1.Get("/update",hand) v1.Get("/delete",hand) } ``` ## 定义错误信息 ``` //任何错误信息都返回 此错误 app.OnAnyErrorCode(func(ctx iris.Context) { ctx.WriteString("see is error") }) //根据状态码返回错误 app.OnErrorCode(httptest.StatusInternalServerError, func(ctx iris.Context) { ctx.WriteString("服务器内部错误") }) ``` ## 异常恢复 ``` ```