美文网首页
go web开发之iris(三)路由

go web开发之iris(三)路由

作者: 东京的雨不会淋湿首尔 | 来源:发表于2019-07-17 18:54 被阅读0次

1.普通路由

package main

import "github.com/kataras/iris"

func main() {
   app := iris.New()
   //GET 方法
   app.Get("/", handler)
   // POST 方法
   app.Post("/", handler)
   // PUT 方法
   app.Put("/", handler)
   // DELETE 方法
   app.Delete("/", handler)
   //OPTIONS 方法
   app.Options("/", handler)
   //TRACE 方法
   app.Trace("/", handler)
   //CONNECT 方法
   app.Connect("/", handler)
   //HEAD 方法
   app.Head("/", handler)
   // PATCH 方法
   app.Patch("/", handler)
   //任意的http请求方法如option等
   app.Any("/", handler)

   app.Run(iris.Addr(":8085"),iris.WithCharset("UTF-8"))
}

// 处理函数
func handler(ctx iris.Context) {
   ctx.Writef("methdo:%s path:%s",ctx.Method(),ctx.Path())
}

2.路由分组

package main

import "github.com/kataras/iris"

func main()  {
   app := iris.New()

   // 分组
   userRouter := app.Party("/user")
   // route: /user/{name}/home  例如:/user/dollarKiller/home
   userRouter.Get("/{name:string}/home", func(ctx iris.Context) {
      name := ctx.Params().Get("name")
      ctx.Writef("you name: %s",name)
   })
   // route: /user/post
   userRouter.Post("/post", func(ctx iris.Context) {
      ctx.Writef("method:%s,path;%s",ctx.Method(),ctx.Path())
   })


   app.Run(iris.Addr(":8085"),iris.WithCharset("UTF-8"))
}

3.动态路由

func main() {
   app := iris.New()

   // 路由传参
   app.Get("/username/{name}", func(ctx iris.Context) {
      name := ctx.Params().Get("name")
      fmt.Println(name)
   })

   // 设置参数
   app.Get("/profile/{id:int min(1)}", func(ctx iris.Context) {
      i, e := ctx.Params().GetInt("id")
      if e != nil {
         ctx.WriteString("error you input")
      }

      ctx.WriteString(strconv.Itoa(i))
   })

   // 设置错误码
   app.Get("/profile/{id:int min(1)}/friends/{friendid:int max(8) else 504}", func(ctx iris.Context) {
      i, _ := ctx.Params().GetInt("id")
      getInt, _ := ctx.Params().GetInt("friendid")
      ctx.Writef("Hello id:%d looking for friend id: ",i,getInt)
   })// 如果没有传递所有路由的macros,这将抛出504错误代码而不是404.

   // 正则表达式
   app.Get("/lowercase/{name:string regexp(^[a-z]+)}", func(ctx iris.Context) {
      ctx.Writef("name should be only lowercase, otherwise this handler will never executed: %s", ctx.Params().Get("name"))
   })

   app.Run(iris.Addr(":8085"),iris.WithCharset("UTF-8"))
}

相关文章

  • go web开发之iris(三)路由

    1.普通路由 2.路由分组 3.动态路由

  • go web开发之iris(二)初识iris

    iris不做介绍,相关的信息可以在github上找到。iris的特点就是简单、全面、易于上手。先来看一个简单的例子...

  • go web开发之iris(一)

    1.vgo安装:https://github.com/wuyumin/vgo/blob/master/docs/z...

  • Go web 开发框架 Iris

    背景 掌握了 Go 语言的基础后就该开始实践了,编写Web应用首先需要一个 web 开发框架。做框架选型时,处理w...

  • 从hello-world开始go iris

    从hello-world开始go iris iris框架是少数支持MVC的go web框架。在简单业务逻辑测试中,...

  • go web开发之iris(六)MVC基础

    这种模式类似于基于类(view)的结构。相比于前面函数式的编码方式来书,逻辑性更强,结构也更加清晰。 配置方式1 ...

  • go web开发之url路由设计

    概述 最近在搞自己的go web开发框架, 反正也没打算私藏, 所以现在先拿出url路由设计这块来写一篇博客. 做...

  • go web开发之iris(四)中间件Middleware

    中间件主要用来处理页面的登录校验、跨站请求伪造防御、日志记录、session设置,权限管理等。例如:可以对所有请求...

  • Iris框架认识

    关于Iris Iris是一个通过GO编写的快速的,简单的,但是功能齐全和非常有效率的web框架Iris为你下一个网...

  • Go Web编程.epub

    【下载地址】 《Go Web编程》介绍如何用Go语言进行Web应用的开发,将Go语言的特性与Web开发实战组合到一...

网友评论

      本文标题:go web开发之iris(三)路由

      本文链接:https://www.haomeiwen.com/subject/nwvclctx.html