go初识iris框架(三) - 路由功能处理方式

继了解get,post后

package main

import "github.com/kataras/iris/v12"

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

	//app.Handle(请求方式,url,请求方法)
	app.Handle("GET","/userinfo",func(ctx iris.Context){
    
    
		path := ctx.Path()
		app.Logger().Info(path) //获取uri路径
	})

	app.Listen(":8083")
}

localhost:8083/userinfo
在这里插入图片描述

日志打印

app := iris.New()
app.Handle("GET","/userinfo",func(ctx iris.Context){
    
    
		path := ctx.Path()
		app.Logger().Info(path) //获取uri路径。 info日志
		app.Logger().Error()	//error日志
	})

Get正则表达式路由

package main

import "github.com/kataras/iris/v12"

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

	app.Get("/hello/{name}",func(ctx iris.Context){
    
    
		path := ctx.Path()
		app.Logger().Info(path)
		//获取参数
		name := ctx.Params().Get("name")
		ctx.HTML("<h1>"+name+"</h1>") //显示
	})

	app.Listen(":8083")
}

localhost:8083/hello/10
在这里插入图片描述

自定义正则表达式

package main

import "github.com/kataras/iris/v12"

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

	app.Get("/hello/{isLogin:bool}",func(ctx iris.Context){
    
    
		isLogin,err := ctx.Params().GetBool("isLogin")
		if err != nil{
    
    
			ctx.StatusCode(iris.StatusNonAuthoritativeInfo)
			return
		}
		if isLogin {
    
    
			
		}
	})

	app.Listen(":8083")
}

猜你喜欢

转载自blog.csdn.net/yasinawolaopo/article/details/131944162